3 回答

TA貢獻(xiàn)1812條經(jīng)驗(yàn) 獲得超5個(gè)贊
正如已經(jīng)回答的那樣,這是不可能的,因?yàn)?Golang 不(并且可能不會(huì))支持方法重載。
使用其他語(yǔ)言的經(jīng)驗(yàn)告訴我們,具有相同名稱但不同簽名的各種方法偶爾有用,但在實(shí)踐中也可能令人困惑和脆弱。僅按名稱匹配并要求類型一致是 Go 類型系統(tǒng)中的一個(gè)主要簡(jiǎn)化決策。

TA貢獻(xiàn)1779條經(jīng)驗(yàn) 獲得超6個(gè)贊
這不可能。
在 go 中,你必須有一個(gè)單一的方法簽名。
您應(yīng)該重命名一種方法。

TA貢獻(xiàn)1757條經(jīng)驗(yàn) 獲得超7個(gè)贊
方法簽名必須匹配。如果你想要依賴注入,我會(huì)推薦功能選項(xiàng)模式。功能選項(xiàng)是返回在構(gòu)造函數(shù)的循環(huán)中調(diào)用的其他函數(shù)的函數(shù)。這是一個(gè)如何使用功能選項(xiàng)和接口基礎(chǔ)知識(shí)的示例。
package main
import (
"fmt"
"strconv"
)
type SomeData struct {
data string
}
// SomeData and SomeOtherData both implement SomeInterface and SomeOtherInterface
// SomeInterface and SomeOtherInterface both implement each other.
type SomeInterface interface {
String() string
Set(data string)
}
func (s *SomeData)String() string {
return s.data
}
func (s *SomeData)Set(data string) {
s.data = data
}
// SetDataOption is a functional option that can be used to inject a constructor dep
func SetDataOption(data string) func(*SomeData) {
return func(s *SomeData) {
s.Set(data)
}
}
// NewSomeData is the constructor; it takes in 0 to many functional options and calls each one in a loop.
func NewSomeData(options ...func(s *SomeData)) SomeInterface {
s := new(SomeData)
for _, o := range options {
o(s)
}
return s
}
//********************
type SomeOtherData struct {
data string
i int
}
type SomeOtherInterface interface {
String() string
Set(data string)
}
func (s *SomeOtherData)String() string {
return s.data + " " + strconv.Itoa(s.i)
}
func (s *SomeOtherData)Set(data string) {
s.data = data
}
func SetOtherDataOption(data string) func(*SomeOtherData) {
return func(s *SomeOtherData) {
s.Set(data)
}
}
func SetOtherIntOption(i int) func(*SomeOtherData) {
return func(s *SomeOtherData) {
s.i = i
}
}
// NewSomeOther data works just like NewSomeData only in this case, there are more options to choose from
// you can use none or any of them.
func NewSomeOtherData(options ...func(s *SomeOtherData)) SomeOtherInterface {
s := new(SomeOtherData)
for _, o := range options {
o(s)
}
return s
}
//*********************************
// HandleData accepts an interface
// Regardless of which underlying struct is in the interface, this function will handle
// either by calling the methods on the underlying struct.
func HandleData(si SomeInterface) {
fmt.Println(si) // fmt.Println calls the String() method of your struct if it has one using the Stringer interface
}
func main() {
someData := NewSomeData(SetDataOption("Optional constructor dep"))
someOtherData := NewSomeOtherData(SetOtherDataOption("Other optional constructor dep"), SetOtherIntOption(42))
HandleData(someData) // calls SomeData.String()
HandleData(someOtherData) // calls SomeOtherData.String()
someOtherData = someData // assign the first interface to the second, this works because they both implement each other.
HandleData(someOtherData) // calls SomeData.String() because there is a SomeData in the someOtherData variable.
}
- 3 回答
- 0 關(guān)注
- 227 瀏覽
添加回答
舉報(bào)