繁星点点滴滴
您可以使用带有按值传递的函数,并根据需要返回未更改或更改的参数。使用上面的结构:func main() { copyOf := func(y Person) Person { y.name = "Polonius" y.address = append(y.address, Address{ city: "other city", state: "other state", }) return y } p := Person{ age: 20, name: "Jack", address: []Address{ { city: "city1", state: "state1", }, { city: "city2", state: "state2", }, }, } q := copyOf(p) fmt.Printf("Orig %v, \naddrss: %p \n\n", p, &p) fmt.Printf("Copy %v, \naddress: %p\n\n", q, &q)}