golang结构体的初识
package mainimport "fmt"
func main() {
//结构体
// 方法一--------------------------------------------------------------------------------------------
var p1 Person
fmt.Println(p1)
p1.name = "王二狗"
p1.age = 30
p1.sex = "男"
p1.address = "赤峰市"
fmt.Printf("姓名:%s\n年龄:%d\n性别:%s\n地址:%s\n", p1.name, p1.age, p1.sex, p1.address)
fmt.Println("------------------------------")
//方法二--------------------------------------------------------------------------------------------
p2 := Person{}
p2.name = "ruby"
p2.age = 28
p2.sex = "女"
p2.address = "内蒙古"
fmt.Printf("姓名:%s\n年龄:%d\n性别:%s\n地址:%s\n", p2.name, p2.age, p2.sex, p2.address)
fmt.Println("------------------------------")
//方法三--------------------------------------------------------------------------------------------
p3 := Person{name: "如花", age: 33, sex: "女", address: "北京市"}
fmt.Printf("姓名:%s\n年龄:%d\n性别:%s\n地址:%s\n", p3.name, p3.age, p3.sex, p3.address)
fmt.Println("------------------------------")
p4 := Person{
name: "老王",
age: 33,
sex: "男",
address: "上海市",
}
fmt.Printf("姓名:%s\n年龄:%d\n性别:%s\n地址:%s\n", p4.name, p4.age, p4.sex, p4.address)
fmt.Println("------------------------------")
//方法四--------------------------------------------------------------------------------------------
p5 := Person{"李小花", 25, "女", "北京上海市"}
fmt.Printf("姓名:%s\n年龄:%d\n性别:%s\n地址:%s\n", p5.name, p5.age, p5.sex, p5.address)
}
type Person struct {
name string
age int
sex string
address string
}
页:
[1]