- 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.
- Java逍遙游記
- Puppet 4 Essentials(Second Edition)
- Google Flutter Mobile Development Quick Start Guide
- Linux核心技術(shù)從小白到大牛
- Mastering Scientific Computing with R
- Python面向?qū)ο缶幊蹋簶?gòu)建游戲和GUI
- 高級(jí)語(yǔ)言程序設(shè)計(jì)(C語(yǔ)言版):基于計(jì)算思維能力培養(yǎng)
- Apache Kafka Quick Start Guide
- Unity UI Cookbook
- Spring技術(shù)內(nèi)幕:深入解析Spring架構(gòu)與設(shè)計(jì)原理(第2版)
- 一步一步跟我學(xué)Scratch3.0案例
- 官方 Scratch 3.0 編程趣味卡:讓孩子們愛上編程(全彩)
- SFML Game Development
- Unreal Engine Game Development Cookbook
- Spring MVC Blueprints