How to use golang interface

PHPz
Release: 2023-04-24 10:32:49
Original
589 people have browsed it

In Golang, an interface (Interface) is a type that defines the set of methods that an object should have. Using interfaces allows us to describe objects in an abstract way without worrying about the specific implementation of the object.

The declaration format of the interface is as follows:

type interface name interface {

方法1(参数1 类型1, 参数2 类型2, ...) (返回值1 类型1, 返回值2 类型2, ...)
方法2(参数1 类型1, 参数2 类型2, ...) (返回值1 类型1, 返回值2 类型2, ...)
...
Copy after login

}

Among them, the interface name can be any legal identifier; method Can have zero or more parameters and zero or more return values, and the types of parameters and return values ​​can be any type, including interface types.

The following is a simple example that demonstrates how to define an interface and use it to describe two different types:

package main

import "fmt"

// Define the interface
type Shape interface {

Area() float64
Copy after login

}

// Define the structure Circle
type Circle struct {

X, Y, Radius float64
Copy after login

}

// Define the method Area() to implement the Shape interface
func (c Circle) Area() float64 {

return 3.14 * c.Radius * c.Radius
Copy after login

}

// Define the structure Rectangle
type Rectangle struct {

X1, Y1, X2, Y2 float64
Copy after login

}

// Define the method Area() to implement the Shape interface
func (r Rectangle) Area() float64 {

return (r.X2 - r.X1) * (r.Y2 - r.Y1)
Copy after login

}

// Define the function GetArea, which accepts a Shape type parameter and calls its Area() method
func GetArea(shape Shape) float64 {

return shape.Area()
Copy after login

}

func main() {

// 创建一个 Circle 对象并计算它的面积
c := Circle{0, 0, 5}
fmt.Println(GetArea(c)) // 输出:78.5

// 创建一个 Rectangle 对象并计算它的面积
r := Rectangle{0, 0, 10, 10}
fmt.Println(GetArea(r)) // 输出:100

// 注意:Circle 和 Rectangle 类型都实现了 Shape 接口,因此可以作为参数传递给 GetArea 函数
Copy after login

}

In the above example, we defined an interface named Shape and defined two different Types Circle and Rectangle, they all implement the Area() method of the Shape interface. We also define a function called GetArea that accepts a parameter of type Shape and calls its Area() method to calculate the area.

In the main function, we created a Circle object and a Rectangle object respectively, and passed them to the GetArea function to calculate their areas. Since both the Circle and Rectangle types implement the Shape interface, they can both be passed as parameters to the GetArea function to calculate the area.

In short, interface is a very important concept in Golang. It allows us to describe objects in an abstract way without worrying about the specific implementation of the object. By mastering the use of interfaces, we can better design and write Golang code.

The above is the detailed content of How to use golang interface. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template