我试图理解为什么如预期下面的测试代码是不工作:为什么不通过方法对结构进行更改仍然存在?
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"
}
这将打印:
"1"
"0"
意思就是说someStrings
显然修改...然后它不是。
有人知道可能是什么问题?