官术网_书友最值得收藏!

Interfaces

Interfaces are an advanced Go feature, which means that you might not want to use them in your programs if you are not feeling very comfortable with Go. However, interfaces can be very practical when developing big Go programs, which is the main reason for talking about interfaces in this book.

But first, I will talk about methods, which are functions with a special receiver argument. You declare methods as ordinary functions with an additional parameter that appears just before the function name. This particular parameter connects the function to the type of that extra parameter. As a result, that parameter is called the receiver of the method. You will see such functions in a while.

Put simply, interfaces are abstract types that define a set of functions that need to be implemented so that a type can be considered an instance of the interface. When this happens, we say that the type satisfies this interface. So, an interface is two things--a set of methods and a type--and it is used for defining the behavior of a type.

Let's describe the main advantage of interfaces with an example. Imagine that you have a type named ATYPE and an interface for the ATYPE type. Any function that accepts an ATYPE variable can accept any other variable that implements the interface of ATYPE.

The Go code of interfaces.go can be divided into three parts. The first part is as follows:

package main 
 
import ( 
   "fmt" 
) 
 
type coordinates interface { 
   xaxis() int 
   yaxis() int 
} 
 
type point2D struct { 
   X int 
   Y int 
} 

In this part, you define an interface called coordinates and a new structure called point2D. The interface has two functions, named xaxis() and yaxis(). The definition of the coordinates interface says that if you want to convert to the coordinates interface, you will have to implement these two functions.

It is important to notice that the interface does not state any other specific types apart from the interface itself. On the other hand, the two functions of the interface should state the types of their return values.

The second part has the following Go code:

func (s point2D) xaxis() int { 
   return s.X 
} 
 
func (s point2D) yaxis() int { 
   return s.Y 
} 
 
func findCoordinates(a coordinates) { 
   fmt.Println("X:", a.xaxis(), "Y:", a.yaxis()) 
} 
 
type coordinate int 
 
func (s coordinate) xaxis() int { 
   return int(s) 
} 
 
func (s coordinate) yaxis() int { 
   return 0 
} 

In the second part, you first implement the two functions of the coordinates interface for the point2D type. Then you develop a function named findCoordinates() that accepts a variable that implements the coordinates interface. The findCoordinates() function just prints the two coordinates of a point using a simple fmt.Println() function call. Then, you define a new type named coordinate that is used for points that belong to the x-axis. Last, you implement the coordinates interface for the coordinate type.

At the time of writing the code for interfaces.go, I believed that the coordinates and coordinate names were fine. After writing the previous paragraph, I realized that the coordinate type could have been renamed to xpoint for better readability. I left the names coordinates and coordinate to point out that everybody makes mistakes and that the variable and type names you are using must be chosen wisely.

The last part has the following Go code:

func main() { 
 
   x := point2D{X: -1, Y: 12}
   fmt.Println(x) 
   findCoordinates(x) 
 
   y := coordinate(10) 
   findCoordinates(y) 
} 

In this part, you first create a point2D variable and print its coordinates using the findCoordinates() function, then you create a coordinate variable named y that holds a single coordinate value. Lastly, you print the y variable using the same findCoordinates() function used for printing a point2D variable.

Although Go is not an object-oriented programming language, I will use some object-oriented terminology here. So, in object-oriented terminology, this means that both point2D and coordinate types are coordinate objects. However, none of them are only a coordinate object.

Executing interfaces.go creates the following output:

$ go run interfaces.go
{-1 12}
X: -1 Y: 12
X: 10 Y: 0

I believe that Go interfaces are not necessary when developing systems software, but they are a handy Go feature that can make the development of a systems application more readable and simpler, so do not hesitate to use them.

主站蜘蛛池模板: 保定市| 榆树市| 仪陇县| 德清县| 旬阳县| 麟游县| 瑞丽市| 天气| 鸡西市| 同江市| 延寿县| 大洼县| 临猗县| 永济市| 民丰县| 土默特左旗| 自贡市| 湟中县| 芦山县| 莱州市| 永登县| 开封县| 青冈县| 莎车县| 烟台市| 巨鹿县| 沙坪坝区| 三门县| 麟游县| 偏关县| 虞城县| 紫阳县| 肥西县| 青海省| 辉南县| 平利县| 大庆市| 武平县| 金沙县| 罗城| 肇东市|