向嵌套结构体添加间接值。例如,如果结构体Person包含一个嵌套结构体Address,则可以使用间接引用将值添加到Address字段中。以下是示例代码:
type Address struct {
City string
State string
}
type Person struct {
Name string
Age int
Address *Address
}
func main() {
addr := &Address{City: "Beijing", State: "China"}
person := &Person{Name: "Tom", Age: 20, Address: addr}
// 使用间接引用向Address结构体添加值
person.Address.City = "Shanghai"
person.Address.State = "China"
}