package main
import "fmt"
type Human struct {
Sex int
Age int
From string
}
func getHumanStruct() {
// ====== 方式一:使用var ======
// var personal Human
// personal.Sex = 1
// personal.Age = 26
// personal.From = "China"
// fmt.Println(personal)
// ====== 方式二:使用匿名的方式 省略了var关键字 ======
// personal := Human{Sex: 1, Age: 26, From: "China"}
// fmt.Println(personal)
// ====== 方式三:new的方式 ======
personal := new(Human) // 返回指针
personal.Sex = 1
personal.Age = 26
personal.From = "China"
fmt.Println(personal)
}
func main() {
getHumanStruct()
}
// 输出
MacintoshdeMacBook-Pro-139:struct elasticnotes$ go run struct.go
{1 26 China}
MacintoshdeMacBook-Pro-139:struct elasticnotes$ go run struct.go
{1 26 China}
MacintoshdeMacBook-Pro-139:struct elasticnotes$ go run struct.go
&{1 26 China}