go interface 的坑
生活随笔
收集整理的這篇文章主要介紹了
go interface 的坑
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一、概述
1 [root@node175 demo]# tree 2 . 3 ├── lib 4 │?? └── world.go 5 ├── README 6 └── server.go 7 8 1 directory, 3 files 9 10 #server.go code 11 package main 12 13 import "fmt" 14 import "demo/lib" 15 16 type MyPrint struct { 17 name string 18 } 19 20 type Interface interface { 21 Print(string) error 22 } 23 24 func (this *MyPrint) Print(who string) error { 25 fmt.Printf("%s name is %s\n", who, this.name) 26 return nil 27 } 28 29 func MyselfPrint(name string) Interface { 30 return MyPrint{name: name} 31 } 32 33 func main() { 34 fmt.Println("Hi, " + lib.World()) 35 MyInterface := MyselfPrint("bob") 36 fmt.Printf("MyInterface type: %T\n", MyInterface) 37 MyInterface.Print("my") 38 }運行:
[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的形式來調用方法,并不是表明值類型的對象擁有該方法。
換一個維度來看上面的表格可能更加直觀:
1 Methods Receivers Values 2 ----------------------------------------------- 3 (t T) T and *T 4 5 (t *T) *T這就意味著指針類型的receiver 方法實現接口時,只有指針類型的對象實現了該接口。
對應上面的例子來說,只有&MyPrint實現了Interface接口,而MyPrint根本沒有實現該接口。所以上面代碼會報出這樣的異常。
1 MyPrint method has pointer receiver 解決這個問題也很容易,直接使用&MyPrint去代替MyPrint調用方法即可:1 package main 2 3 import "fmt" 4 import "demo/lib" 5 6 type MyPrint struct { 7 name string 8 } 9 10 type Interface interface { 11 Print(string) error 12 } 13 14 func (this *MyPrint) Print(who string) error { 15 fmt.Printf("%s name is %s\n", who, this.name) 16 return nil 17 } 18 19 func MyselfPrint(name string) Interface { 20 return &MyPrint{name: name} 21 } 22 23 func main() { 24 fmt.Println("Hi, " + lib.World()) 25 MyInterface := MyselfPrint("bob") 26 fmt.Printf("MyInterface type: %T\n", MyInterface) 27 MyInterface.Print("my") 28 }
或者:
1 package main 2 3 import "fmt" 4 import "demo/lib" 5 6 type MyPrint struct { 7 name string 8 } 9 10 type Interface interface { 11 Print(string) error 12 } 13 14 func (this MyPrint) Print(who string) error { 15 fmt.Printf("%s name is %s\n", who, this.name) 16 return nil 17 } 18 19 func MyselfPrint(name string) Interface { 20 return MyPrint{name: name} 21 } 22 23 func main() { 24 fmt.Println("Hi, " + lib.World()) 25 MyInterface := MyselfPrint("bob") 26 fmt.Printf("MyInterface type: %T\n", MyInterface) 27 MyInterface.Print("my") 28 } ?再或者: 1 package main 2 3 import "fmt" 4 import "demo/lib" 5 6 type MyPrint struct { 7 name string 8 } 9 10 type Interface interface { 11 Print(string) error 12 } 13 14 func (this MyPrint) Print(who string) error { 15 fmt.Printf("%s name is %s\n", who, this.name) 16 return nil 17 } 18 19 func MyselfPrint(name string) Interface { 20 return &MyPrint{name: name} 21 } 22 23 func main() { 24 fmt.Println("Hi, " + lib.World()) 25 MyInterface := MyselfPrint("bob") 26 fmt.Printf("MyInterface type: %T\n", MyInterface) 27 MyInterface.Print("my") 28 }?
總結
以上是生活随笔為你收集整理的go interface 的坑的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 玩转 React(二)- 新型前端开发方
- 下一篇: 六大举措建云管理模式助力企业转型升级