- Go Machine Learning Projects
- Xuanyi Chew
- 295字
- 2021-06-10 18:46:33
Interfaces
An interface is a set of methods. We can define an interface by listing out the methods it's expected to support. For example, consider the following interface:
var a interface {
check()
}
Here we are defining a to be a variable that has the type interface{ check() }. What on earth does that mean?
It means that you can put any value into a, as long as the value has a type that has a method called check().
Why is this valuable? It's valuable when considering multiple types that do similar things. Consider the following:
type complicatedEmail struct {...}
func (e complicatedEmail) check() {...}
func (e complicatedEmail) send(a string) {...}
type simpleEmail string
func (e simpleEmail) check() {...}
func (e simpleEmail) send(a string) {...}
Now we want to write a function do, which does two things:
- Check that an email address is correct
- Send "Hello World" to the email
You would need two do functions:
func doC(a complicatedEmail) {
a.check()
a.send("Hello World")
}
func doS(a simpleEmail) {
a.check()
a.send("Hello World")
}
Instead, if that's all the bodies of the functions are, we may opt to do this:
func do(a interface{
check()
send(a string)
}) {
a.check()
a.send("Hello World")
}
This is quite hard to read. So let's give the interface a name:
type checkSender interface{
check()
send(a string)
}
Then we can simply redefine do to be the following:
func do(a checkSender) {
a.check()
a.send("Hello World")
}
A note on naming interfaces in Go. It is customary to name interfaces with a -er suffix. If a type implements check(), then the interface name should be called checker. This encourages the interfaces to be small. An interface should only define a small number of methods—larger interfaces are signs of poor program design.
- 數據運營之路:掘金數據化時代
- 電腦上網直通車
- 運動控制器與交流伺服系統的調試和應用
- 統計學習理論與方法:R語言版
- Implementing Oracle API Platform Cloud Service
- INSTANT Drools Starter
- Implementing AWS:Design,Build,and Manage your Infrastructure
- Learn CloudFormation
- 新編計算機圖形學
- Mastering GitLab 12
- Flink原理與實踐
- Learning ServiceNow
- Getting Started with Tableau 2019.2
- ASP.NET 4.0 MVC敏捷開發給力起飛
- Learning VMware App Volumes