前言

根据使用者的反馈,回答关于开源项目:go-gin-api 中被频繁问到的两个代码写法问题。

以如下代码片段为例:

two.png

  1. 第 8 行,这种写法是什么意思?
  2. 第 11 行,为什么定义一个 i() 方法?

问题一

  1. var _ Signature = (*signature)(nil)

这代码是什么意思?

强制 *signature 去实现 Signature 接口,编译器会检查 *signature 类型是否实现了 Signature 接口。

来看一个例子:

  1. package main
  2. import "fmt"
  3. var _ Study = (*study)(nil)
  4. type study struct {
  5. Name string
  6. }
  7. type Study interface {
  8. Listen(message string) string
  9. }
  10. func main() {
  11. fmt.Println("hello world")
  12. }

上面代码会输出 hello world 吗?

不会!

这时会输出:

  1. ./main.go:5:5: cannot use (*study)(nil) (type *study) as type Study in assignment:
  2. *study does not implement Study (missing Listen method)

如果去掉这一行:

  1. var _ Study = (*study)(nil)

这时就可以输出 hello world 了。

问题二

  1. i()

为什么在接口中定义一个 i() 方法?

强制 Signature 接口中所有方法只能在本包中去实现,在其他包中不允许去实现。因为接口中有小写方法,所以在其他包无法去实现。i() 表示一个小写方法,起其他名字也可以。

来看一个例子:

  1. package study
  2. type Study interface {
  3. Listen(message string) string
  4. i()
  5. }
  6. func Speak(s Study) string {
  7. return s.Listen("abc")
  8. }

定义了一个 Study 接口,接口中包含两个方法,其中就有一个 i() 。

定义了一个 Speak 方法,入参是 Study 接口,方法体是执行接口中的 Listen 方法。

接下来看另一个文件:

  1. type stu struct {
  2. Name string
  3. }
  4. func (s *stu) Listen(message string) string {
  5. return s.Name + " 听 " + message
  6. }
  7. func (s *stu) i() {}
  8. func main() {
  9. message := study.Speak(new(stu))
  10. fmt.Println(message)
  11. }

定义了一个 stu 结构体,这个结构体实现了 Study 接口中的方法,那么上述程序会正常输出吗?

不会!

这时会输出:

  1. ./main.go:19:28: cannot use new(stu) (type *stu) as type study.Study in argument to study.Speak:
  2. *stu does not implement study.Study (missing study.i method)
  3. have i()
  4. want study.i()

如果去掉接口中 i(),会正常输出:听 abc

小结

以上两个是读者在学习代码的过程中最常问的问题,希望这次能够帮大家解惑。

另外 option 设计模式,问的也不少,大家可以看下这篇文章:《函数的不定参数你是这样用吗?

推荐阅读