我正在用泛型做方法,它会从泛型指针获取或设置一些东西,我做错了什么?
package main
import "fmt"
type IPlayer[W any, I any] struct {
ID string
Other W
Something I
}
func (ply *IPlayer[W, I]) Set(str string) {
temp := new(IPlayer[W, I])
temp.ID = str
ply = temp
}
func main() {
user := new(IPlayer[string, string])
user.ID = "before"
fmt.Println(user.ID)
user.Set("test")
fmt.Println(user.ID)
}
我期待
before
test
但我得到了
before
before