我试图理解为什么以下测试代码没有按预期工作:
package main
import (
"fmt"
"strings"
)
type Test struct {
someStrings []string
}
func (this Test) AddString(s string) {
this.someStrings = append(this.someStrings, s)
this.Count() // will print "1"
}
func (this Test) Count() {
fmt.Println(len(this.someStrings))
}
func main() {
var test Test
test.AddString("testing")
test.Count() // will print "0"
}
Run Code Online (Sandbox Code Playgroud)
这将打印:
"1"
"0"
Run Code Online (Sandbox Code Playgroud)
意味着someStrings明显被修改......然后它不是.
有人知道可能是什么问题吗?