亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

從不同的包golang實現接口

從不同的包golang實現接口

Go
揚帆大魚 2021-03-29 13:15:08
我在嘗試實現接口(在golang的其他程序包中定義)時遇到了一些問題。我對下面的問題做了些微的闡述界面:package interfacestype Interface interface {    do(param int) int}執行:package implementationstype Implementation struct{}func (implementation *Implementation) do(param int) int {    return param}Main.go:package mainimport (    "test/implementing-interface-in-different-package/implementations"    "test/implementing-interface-in-different-package/interfaces")func main() {    var interfaceImpl interfaces.Interface    interfaceImpl = &implementations.Implementation{}}錯誤信息:test/implementing-interface-in-different-package./main.go:10:16: cannot use implementations.Implementation literal (type implementations.Implementation) as type interfaces.Interface in assignment:    implementations.Implementation does not implement interfaces.Interface (missing interfaces.do method)            have implementations.do(int) int            want interfaces.do(int) int是否可以從其他包中實現接口?
查看完整描述

1 回答

?
森林海

TA貢獻2011條經驗 獲得超2個贊

問題在于您的do函數沒有從implementations包中導出,因為它以小寫字母開頭。因此,從包的角度來看main,變量interfaceImpl無法實現接口,因為它看不到do函數。

重命名您的接口函數為大寫字母Do以解決此問題。


查看完整回答
反對 回復 2021-04-19
  • 1 回答
  • 0 關注
  • 1089 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號