Golang 通用指针无法设置 [重复]

问题描述 投票:0回答:0

我正在用泛型做方法,它会从泛型指针获取或设置一些东西,我做错了什么?

游乐场

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
go pointers generics
© www.soinside.com 2019 - 2024. All rights reserved.