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

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.

主站蜘蛛池模板: 合江县| 米易县| 海城市| 太湖县| 灵璧县| 辽阳县| 天镇县| 亚东县| 佛冈县| 丹东市| 锦屏县| 阳高县| 博兴县| 镇原县| 牡丹江市| 历史| 蒙山县| 盘锦市| 芦山县| 望都县| 北碚区| 沽源县| 太湖县| 政和县| 陇川县| 额济纳旗| 华亭县| 霸州市| 苏尼特左旗| 青浦区| 宁津县| 重庆市| 香格里拉县| 墨竹工卡县| 吴旗县| 皮山县| 平山县| 米脂县| 遂宁市| 眉山市| 益阳市|