You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
26 lines
459 B
26 lines
459 B
package main
|
|
|
|
import "fmt"
|
|
|
|
type Person3 struct {
|
|
name string
|
|
age int8
|
|
dreams []string
|
|
}
|
|
|
|
func (p *Person3) SetDreams(dreams []string) {
|
|
//p.dreams = dreams
|
|
p.dreams = make([]string, len(dreams))
|
|
copy(p.dreams, dreams)
|
|
}
|
|
|
|
func main() {
|
|
p1 := Person3{name: "小王子", age: 18}
|
|
data := []string{"吃饭", "睡觉", "打豆豆"}
|
|
p1.SetDreams(data)
|
|
|
|
// 你真的想要修改 p1.dreams 吗?
|
|
data[1] = "不睡觉"
|
|
fmt.Println(p1.dreams) // ?
|
|
}
|