Golang程序 创建接口
在这篇文章中,我们将学习如何使用golang编程来创建一个接口。
接口 - 在go语言中,接口是一种自定义类型,用来指定一个或多个方法签名。一个接口是抽象的,也就是说,我们不能创建一个接口的实例,但是我们可以为接口类型创建一个变量,并将这个变量分配给拥有接口所需方法的结构或类。
语法
type interface_name interface {
// method signatures
}
例子1
要定义一个接口,首先我们需要使用type关键字,这表明我们正在定义一个新的类型,我们需要决定接口的名称,然后是interface关键字,然后我们需要提到这个接口中的方法签名。
package main
import "fmt"
// package fmt allows us to print anything on the screen
// creating a structure named temp and assigning it a property of count
type temp struct {
count int
}
// creating an interface named inter
type inter interface {
// defining method signatures in the interface
getCount() int
}
// defining and creating a function to the structure that returns an integer value
func (t temp) getCount() int {
// returning the value of the count
return t.count
}
func main() {
// creating a variable to the interface type
var struct_intr inter
// creating a new instance to the struct and assigning value to it
st := temp{98}
// Assigning the interface variable to the struct so that we could use the functions defined in
// the interface
struct_intr = st
// accessing the getCount() method from the instance to the struct
fmt.Println("Accessing the getCount() method from the st instance \ncount=", st.getCount(), "\n")
// priting the count by calling the getCount() method from the struct_inter interface variable
fmt.Println("Accessing the getCount() method from the interface variable\ncount=", struct_intr.getCount(), "\n")
}
输出
Accessing the getCount() method from the st instance
count= 98
Accessing the getCount() method from the interface variable
count= 98
说明
- 导入fmt包,允许我们在屏幕上打印任何东西。
-
创建一个以count为键的temp结构。
-
创建一个名为inter的接口,并在其中定义方法签名。
-
为该结构定义getCount()函数,返回整数值作为计数。
-
现在我们需要为接口类型定义一个变量。
-
将这个变量分配给上面定义的结构,这将让我们从结构的实例中使用接口中定义的方法。
-
创建一个结构体的实例,并提供一个整数作为计数键的值。
-
现在,我们可以从接口变量和结构的实例中访问 getCount() 方法。
-
先从接口调用 getCount() 方法,然后再从结构变量调用,从而打印出计数值。
示例 2
使用 类型断言 来获取一个具体类型的值,并可以调用另一个接口上定义的方法,但不属于接口满足的范围。
package main
// defining the package’s main
// fmt package allows us to print anything on the screen
import "fmt"
// defining an interface named Polygons and defining method signatures in it
type Polygons interface {
// defining a method signature named Perimeter()
Perimeter()
}
// defining an interface named Object and defining a method in it too
type Object interface {
NumberOfSide()
}
// creating a struct named Pentagon that stores integer values.
type Pentagon int
// defining a method named Perimeter() to the int struct
func (p Pentagon) Perimeter() {
// printing the perimeter on the screen
fmt.Println("Perimeter of Pentagon", 5*p)
}
// defining a method named NumberOfSides() to the int struct
func (p Pentagon) NumberOfSide() {
// getting the number of sides of the pentagon
fmt.Println("A pentagon has 5 sides")
}
func main() {
// Giving the integer value to the pentagon struct
// further assigning the struct to the interface so that we could use the methods defined in it
var p Polygons = Pentagon(50)
// using the perimeter method to print the perimeter of the pentagon on the screen
p.Perimeter()
// assigning the instance to the struct so that we could use the methods present in it.
var o Pentagon = p.(Pentagon)
// using the NumberOfSides() method to calculate the number of sides of the pentagon
o.NumberOfSide()
// Giving the integer value to the pentagon struct
// further assigning the struct to the interface so that we could use the methods defined in it
var obj Object = Pentagon(50)
// using the NumberOfSide() method to print the perimeter of the pentagon on the screen
obj.NumberOfSide()
// printing the perimeter from the obj method
var pent Pentagon = obj.(Pentagon)
pent.Perimeter()
}
输出
Perimeter of Pentagon 250
A pentagon has 5 sides
A pentagon has 5 sides
Perimeter of Pentagon 250
说明
-
导入fmt包,允许我们在屏幕上打印任何东西。
-
创建一个名为Pentagon的新结构,接受整数值。
-
创建两个名为Polygon和object的接口,并在其中定义方法签名。
-
为该结构定义perimeter()和numberOfSides()函数,分别打印五边形的周长和边数。
-
赋予五边形结构以整数值,并将该结构分配给接口,以便我们可以使用其中定义的方法。
-
注意,我们使用的是obj接口中定义的numberOfSIdes(),而不是Polygon。
-
再次,将五边形int分配给五边形结构,并将其分配给obj接口。
-
再一次,我们从obj接口上创建的实例中使用Polygon接口中定义的方法。
-
通过这种方式,我们可以调用定义在其他接口上但不属于该接口满足的方法。