長條貓窩

Back

(Legacy)

以下程式碼中的 Admin 實作了 User

type Admin struct {
	name string
}

func (u Admin) GetName() string {
	return u.name
}

type User interface {
	GetName() string
}

func main() {
	simba := Admin{"simba"}

	// ok
	var user User = simba

	fmt.Println(user.GetName())
}
go

但是以下這段程式就出問題了,因為 User interface 中多了一個 method SetName,而 User 中的 SetName 指定要接收一個指標

type Admin struct {
	name string
}

func (u Admin) GetName() string {
	return u.name
}

func (u *Admin) SetName(name string) {
	u.name = name
}

type User interface {
	GetName() string
	SetName(string)
}

func main() {
	simba := Admin{"simba"}

	// Error: Admin does not implement User (method SetName has pointer receiver)
	var user User = simba

	fmt.Println(user.GetName())

	user.SetName("kenny")

	fmt.Println(user.GetName())
}
go
Pointer to Structure Passed to Interface
https://blog.simbafs.cc/blog/pointer-to-structure-passed-to-interface/
Author Simba Fs
Published at September 27, 2024