问题场景

今天尝试对指针类型进行封装并对外提供判空方法(类似Java8的Optional)的时候,发现Golang要求接收器的基类型不能是指针类型或者接口类型。

等价代码如下:

  1. type Integer *int
  2. func (i Integer) isNil() bool {
  3. if i == nil {
  4. return true
  5. } else {
  6. return false
  7. }
  8. }

Golang文档中对此的说明:https://golang.org/ref/spec#Method_declarations(或者http://docs.studygolang.com/ref/spec#Method_declarations

解决方法

暂无