日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

go interface 的坑

發布時間:2024/4/17 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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 的坑的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。