Golang方法与指针接收器 [英] Golang method with pointer receiver
问题描述
我有这个示例代码
包主
导入(
fmt
)
类型IFace接口{
SetSomeField(newValue字符串)
GetSomeField()字符串
}
类型实现struct {
someField string
}
func(i Implementation)GetSomeField()string {
return i.someField
}
func(i Implementation)SetSomeField(newValue string){
i.someField = newValue
}
func Create()IFace {
obj:= Implementation { someField:Hello}
return obj //< =违规行
}
func main(){
a:= Create()
a.SetSomeField(World)
fmt.Println(a.GetSomeField())
}
$ p
$ p $我将该方法更改为指针接收器,我期望能够工作,它看起来像这样:
func(i *实现)SetSomeField(newValue string){...
编译会导致以下错误:
prog .go:26:在返回参数中不能使用obj(类型实现)作为IFace类型:
实现没有实现IFace(GetSomeField方法有指针接收器)
如何让 struct
实现接口和方法 SetSomeField
在不创建副本的情况下更改实际实例的值
这里有一个可破解的代码片段:
https://play.golang.org/p/ghW0mk0IuU
我已经看到这个问题在go(golang)中,如何将接口指针转换为结构指针?,但我不明白它与这个例子有什么关系。
结构的指针应该实现接口。以这种方式,你可以修改它的字段。
看看我如何修改你的代码,使它按照你的预期工作:
包主要
导入(
fmt
)
IFace界面{
SetSomeField(newValue字符串)
GetSomeField()字符串
}
类型实现struct {
someField字符串
}
func(i *实现)GetSomeField()字符串{
返回i.someField
}
func(i *实现)SetSomeField(newValue string){
i.someField = newValue
}
func Create()* Implementation {
return& Implementation {someField:Hello}
}
func main(){
var a IFace
a = Create()
a.SetSomeField(World)
fmt.Println(a.GetSomeField())
}
I have this example code
package main
import (
"fmt"
)
type IFace interface {
SetSomeField(newValue string)
GetSomeField() string
}
type Implementation struct {
someField string
}
func (i Implementation) GetSomeField() string {
return i.someField
}
func (i Implementation) SetSomeField(newValue string) {
i.someField = newValue
}
func Create() IFace {
obj := Implementation{someField: "Hello"}
return obj // <= Offending line
}
func main() {
a := Create()
a.SetSomeField("World")
fmt.Println(a.GetSomeField())
}
SetSomeField
does not work as expected because its receiver is not of pointer type.
If I change the method to a pointer receiver, what I would expect to work, it looks like this:
func (i *Implementation) SetSomeField(newValue string) { ...
Compiling this leads to the following error:
prog.go:26: cannot use obj (type Implementation) as type IFace in return argument:
Implementation does not implement IFace (GetSomeField method has pointer receiver)
How can I have the struct
implement the interface and the method SetSomeField
change the value of the actual instance without creating a copy?
Here's a hackable snippet: https://play.golang.org/p/ghW0mk0IuU
I've already seen this question In go (golang), how can you cast an interface pointer into a struct pointer?, but I cannot see how it is related to this example.
Your pointer to the struct should implement the Interface. In that way you can modify its fields.
Look at how I modified your code, to make it working as you expect:
package main
import (
"fmt"
)
type IFace interface {
SetSomeField(newValue string)
GetSomeField() string
}
type Implementation struct {
someField string
}
func (i *Implementation) GetSomeField() string {
return i.someField
}
func (i *Implementation) SetSomeField(newValue string) {
i.someField = newValue
}
func Create() *Implementation {
return &Implementation{someField: "Hello"}
}
func main() {
var a IFace
a = Create()
a.SetSomeField("World")
fmt.Println(a.GetSomeField())
}
这篇关于Golang方法与指针接收器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!