- Go Systems Programming
- Mihalis Tsoukalos
- 724字
- 2021-07-02 18:08:00
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.
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.
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.
- Spring Boot開發與測試實戰
- Scala Design Patterns
- C語言程序設計基礎與實驗指導
- 實戰Java程序設計
- Kotlin從基礎到實戰
- 程序設計基礎教程:C語言
- Arduino家居安全系統構建實戰
- 運用后端技術處理業務邏輯(藍橋杯軟件大賽培訓教材-Java方向)
- Python算法詳解
- 51單片機C語言開發教程
- INSTANT Adobe Edge Inspect Starter
- 汽車人機交互界面整合設計
- Nagios Core Administration Cookbook(Second Edition)
- Scala編程實戰
- Mastering Embedded Linux Programming