Go语言结构体struct之创建、初始化)
- 定义struct
初始化
package main import "fmt" func main() { TestForStruct() } //定义一个Dog结构体 type Dog struct { ID int Name string Age int } func TestForStruct() { //方式一 var dog Dog //声明一个变量 //赋值 dog.ID = 1 dog.Name = "didi" dog.Age = 3 fmt.Println(dog) //{1 didi 3} //方式二 dog1 := Dog{ID: 1,Name: "Yay",Age: 2} fmt.Println(dog1) //{1 Yay 2} //方式三 dog2 := new(Dog) dog2.ID = 1 dog2.Name = "GG" dog2.Age = 5 fmt.Println(dog2) //&{1 GG 5} 返回指针类型的 }
Comment here is closed