在Go中,使用结构类型的setter不能按预期工作

cc *_*ung 7 struct go

对结构使用setter函数,但没有按预期工作:

package main

import "fmt"

type T struct { Val string }

// this setter seems not to work
func (t T) SetVal( s string ) {
        t.Val = s
}

// this setter, using ptr to T, seems to work ok
func (t *T) SetVal2( s string ) {
        (*t).Val = s
}

func main() {
        v := T{"abc"}
        fmt.Println( v )        // prints {abc}
        v.SetVal("pdq")
        fmt.Println( v )        // prints {abc}, was expecting {pdq}!
        v.SetVal2("xyz")
        fmt.Println( v )        // prints {xyz}!
}
Run Code Online (Sandbox Code Playgroud)

我缺少一些基本的理解 - 为什么不起作用SetVal

行为类似于设置值reflect,只有在提供指向对象的指针与对象本身时才有效

Dav*_*son 16

以下是您缺少的基本理解:当结构作为指针传递给函数时,该函数可以修改原始结构,因为它有一个指向它的指针.但是,当结构通过其值传递给函数时,实际上只为该函数调用创建了结构的新副本,并且对该结构的新副本的任何修改都不会影响原始结构.

您可以通过打印出有问题的结构的实际地址来证明这是它的工作方式:

package main

import "fmt"

type T struct { Val string }

func (t T) SetVal( s string ) {
        fmt.Printf("Address of copy is %p\n", &t);
}

func (t *T) SetVal2( s string ) {
        fmt.Printf("Pointer argument is %p\n", t);
}

func main() {
        v := T{"abc"}
        fmt.Printf("Address of v is %p\n", &v);
        v.SetVal("pdq")
        v.SetVal2("xyz")
}
Run Code Online (Sandbox Code Playgroud)

当我在Go playground中运行它时,上面的代码会产生这个输出:

Address of v is 0xf840001290
Address of copy is 0xf8400013e0
Pointer argument is 0xf840001290
Run Code Online (Sandbox Code Playgroud)

注意打印出的第一个和第三个指针是如何相等的,这意味着它们是相同的结构.但是第二个指针是不同的,因为它是一个副本.

顺便说一句,这似乎与C结构/函数参数的工作方式完全相同.