package main
import "fmt"
//用户结构体,用来声明对象,不赋值则为类型的初始值
type User struct {
age int
name string
sex string
address string
phone string
hobby []string
}
func main() {
//初始化对象
var user1 User
user1.age = 18
user1.name = "Vitas"
user1.sex = "male"
user1.address = "广东深圳"
user1.phone = "13417572019"
user1.hobby = []string{"篮球","游戏"}
fmt.Print(user1)
fmt.Printf("%v来自%v喜欢%v\n",user1.name,user1.address,user1.hobby)
fmt.Printf("%T\n",user1)
changeInfo(user1)
//修改信息
fmt.Printf("修改后的用户信息为%v\n",user1)
changeInfo1(&user1)
//修改信息
fmt.Printf("真实修改后的用户信息为%v\n",user1)
}
//函数参数是拷贝,也就是副本,改变不了原来的类的值
func changeInfo(user User) {
user.sex = "female"
user.age = 28
}
//如果需要改变值,传指针,根据地址找到原来的值,再去修改
func changeInfo1(user *User) {
//(*user).sex = "female"
//指针是不能修改的,可以直接user去取,会根据指针取到对应的对象
user.sex = "female"
(*user).age = 38
}
打印结果:
image.png
网友评论