- 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.
- C語言程序設(shè)計(第3版)
- OpenShift開發(fā)指南(原書第2版)
- Practical Internet of Things Security
- INSTANT CakePHP Starter
- Groovy for Domain:specific Languages(Second Edition)
- HTML5 and CSS3 Transition,Transformation,and Animation
- Mastering Android Development with Kotlin
- 劍指Java:核心原理與應(yīng)用實踐
- 低代碼平臺開發(fā)實踐:基于React
- Extending Puppet(Second Edition)
- HTML5秘籍(第2版)
- Python程序設(shè)計與算法基礎(chǔ)教程(第2版)(微課版)
- Java并發(fā)編程:核心方法與框架
- Tableau Desktop可視化高級應(yīng)用
- UML軟件建模