type Users struct {
ID int
Name string
}
① *[]Users是一个指针,指向一个数组
var userList []Users
func main() {
// Make the slice of Users
userList = []Users{Users{ID:12345, Name: "Bob"}}
// Then pass the slice as a reference to some function
myFunc(&userList)
fmt.Println(userList)
}
func myFunc(input *[]Users) {
*input = []Users{Users{ID:54321, Name: "Bobby"}}
}
输出
[{54321 Bobby}]
② []*Users是一个数组,里面都是指针类型
var (
user1 Users
user2 Users
)
func main() {
// Make a couple Users
user1 = Users{ID: 123, Name: "Billy"}
user2 = Users{ID: 456, Name: "Boddy"}
// Then make a list of pointers to those users
var userList []*Users = []*Users{&user1, &user2}
// Now you can change an individual users in that list
// This changes the variable user2
*userList[1] = Users{ID: 654, Name: "Bob"}
fmt.Println(user2)
}
输出
{654 Bob}