如何根据泛型类型参数的值进行复制操作
时间:2024-02-13
浏览:317
作者:佚名
各位小伙伴们,大家好呀!看看今天我又给各位带来了什么文章?本文标题是《如何根据泛型类型参数的值进行复制操作》,很明显是关于Golang的文章哈哈哈,其中内容主要会涉及到等等,如果能帮到你,觉得很不错的话,欢迎各位多多点评和分享!
在泛型函数中,如何按值复制具有泛型类型的参数?
这是一个最小的例子:
type MyInterface interface { // Interface use for type constraint
SetID(string)
}
type Implem struct { // Implements MyInterface
ID string
}
func (i *Implem) SetID(id string) {
i.ID = id
}
func doStuff[T MyInterface](input T) T {
res := input // Here I want to make a copy of input, so the next line modify res but not input
res.SetID("newID")
return res
}
func main() {
implem := &Implem{ID: "oldID"}
res := doStuff(implem)
fmt.Println(implem) // Should be "oldID"
fmt.Println(res) // Should be "newID"
}
我尝试了几件事,例如将 res 声明为 *t 并将输入设置为其值,或者使用类型断言,但 setid 仍然修改输入。
按值复制 input 的最佳方法是什么?
正确答案
在一般情况下,浅复制类型参数的工作方式与用于实例化它的任何类型参数相同。如果您使用非指针类型实例化 t,则赋值会复制其值。如果您使用指针类型实例化 t,赋值仍然会复制其值,但该值是内存地址。
在您编写的代码中,指针类型和非指针类型都可以满足类型约束 myinterface 。
假设,您可以声明如下类型:
type foo struct{}
func (i implem) setid(id string) {
}
对值接收器的字段进行任何赋值都是无效的,但 foo 将是有效的类型参数。
也许您想要做的是将类型参数限制为指针类型。这样您就可以捕获基本类型并取消引用以浅复制该值。
// restrict to pointer types
type myinterface[t any] interface {
*t
setid(string)
}
// capture the base type t
// the argument 'input' is a pointer type due to myinterface
func dostuff[t any, u myinterface[t]](input u) u {
t := *input // dereferencing copies the value
res := u(&t) // take address of t and convert to u
res.setid("newid") // you can now call setid on value of type u
return res
}
这个输出
&{oldid}
&{newid}
游乐场:https://go.dev/play/p/u8ssq3_ypvi p>
作为替代方案,您可以向结构添加一个附加方法来返回它的副本。然后在通用函数中,向具有该方法的匿名接口键入断言:
// shallow copy and return same type
func (i *implem) copy() *implem {
c := *i
return &c
}
func dostuff[t myinterface](input t) t {
// type assert to anonymous interface with method that returns t
t, ok := any(input).(interface{ copy() t })
if !ok {
// handle failure
}
res := t.copy()
res.setid("newid")
return res
}
游乐场:https://go.dev/play/p/k7moaphdemm p>
在我看来,使用匿名接口使其更加方便和可读,因为类型参数 t 以不言自明的方式使用。您当然可以定义一个命名接口:
type copier[t any] interface {
copy() t
}
并断言使用 t 实例化的接口:
t, ok := any(input).(Copier[T])
游乐场:https://go.dev/play/p/berisu6qz-p一个>
终于介绍完啦!小伙伴们,这篇关于《如何根据泛型类型参数的值进行复制操作》的介绍应该让你收获多多了吧!欢迎大家收藏或分享给更多需要学习的朋友吧~golang学习网公众号也会发布Golang相关知识,快来关注吧!
本文内容来源于互联网,如有侵权请联系删除。
近期热门
站内热门
最新文章
热门专题
