go interface 的坑

一、概述

 [root@node175 demo]# tree
.
├── lib
│   └── world.go
├── README
└── server.go directory, files #server.go code
package main import "fmt"
import "demo/lib" type MyPrint struct {
name string
} type Interface interface {
Print(string) error
} func (this *MyPrint) Print(who string) error {
fmt.Printf("%s name is %s\n", who, this.name)
return nil
} func NewMyPrint(name string) Interface {
return MyPrint{name: name}
} func main() {
fmt.Println("Hi, " + lib.World())
MyInterface := NewMyPrint("bob")
fmt.Printf("MyInterface type: %T\n", MyInterface)
MyInterface.Print("my")
}

运行:

[root@node175 demo]# go run server.go
# command-line-arguments
./server.go:20: cannot use MyPrint literal (type MyPrint) as type Interface in return argument:
MyPrint does not implement Interface (Print method has pointer receiver)

  为了解决这个问题,首先得先了解一下Golang 中 方法的集合的概念,一个struct虽然可以通过值类型和引用类型两种方式定义方法,但是不同的对象类型对应了不同的方法集:

Values                    Methods Receivers
-----------------------------------------------
T (t T)
*T (t T) and (t *T)

  值类型的对象只有(t T) 结构的方法,虽然值类型的对象也可以调用(t *T) 方法,但这实际上是Golang编译器自动转化成了&t的形式来调用方法,并不是表明值类型的对象拥有该方法。

  换一个维度来看上面的表格可能更加直观:

 Methods Receivers         Values
-----------------------------------------------
(t T) T and *T (t *T) *T

  这就意味着指针类型的receiver 方法实现接口时,只有指针类型的对象实现了该接口。

  对应上面的例子来说,只有&MyPrint实现了Interface接口,而MyPrint根本没有实现该接口。所以上面代码会报出这样的异常。

 MyPrint method has pointer receiver
  解决这个问题也很容易,直接使用&MyPrint去代替MyPrint调用方法即可:
 package main

 import "fmt"
import "demo/lib" type MyPrint struct {
name string
} type Interface interface {
Print(string) error
} func (this *MyPrint) Print(who string) error {
fmt.Printf("%s name is %s\n", who, this.name)
return nil
} func MyselfPrint(name string) Interface {
return &MyPrint{name: name}
} func main() {
fmt.Println("Hi, " + lib.World())
MyInterface := MyselfPrint("bob")
fmt.Printf("MyInterface type: %T\n", MyInterface)
MyInterface.Print("my")
}

或者:

 package main

 import "fmt"
import "demo/lib" type MyPrint struct {
name string
} type Interface interface {
Print(string) error
} func (this MyPrint) Print(who string) error {
fmt.Printf("%s name is %s\n", who, this.name)
return nil
} func MyselfPrint(name string) Interface {
return MyPrint{name: name}
} func main() {
fmt.Println("Hi, " + lib.World())
MyInterface := MyselfPrint("bob")
fmt.Printf("MyInterface type: %T\n", MyInterface)
MyInterface.Print("my")
}
 再或者:
 package main

 import "fmt"
import "demo/lib" type MyPrint struct {
name string
} type Interface interface {
Print(string) error
} func (this MyPrint) Print(who string) error {
fmt.Printf("%s name is %s\n", who, this.name)
return nil
} func MyselfPrint(name string) Interface {
return &MyPrint{name: name}
} func main() {
fmt.Println("Hi, " + lib.World())
MyInterface := MyselfPrint("bob")
fmt.Printf("MyInterface type: %T\n", MyInterface)
MyInterface.Print("my")
}
上一篇:WebService技术,服务端and客户端JDK-wsimport工具(一)


下一篇:[深度分析] Python Web 开发框架 Bottle