funcmain() { var curiosity struct { lat float64 long float64 } curiosity.lat = -4.5859//采用结构体.属性名的方式赋予属性值 curiosity.long = 137.4417 fmt.Println(curiosity.lat) fmt.Println(curiosity.long) }
类型复用结构
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
package main
import"fmt"
funcmain() { type location struct {//声明struct的类型别名location,location类型有两个属性 lat float64 long float64 } var spirit location//声明location类型的变量,改变量自动继承location的两个属性 spirit.lat = -14.5684 spirit.long = 175.4722636
var opportunity location opportunity.lat = -1.9462 opportunity.long = 354.4734
fmt.Println(spirit, opportunity) }
复合字面量初始化结构
1 2 3 4 5 6 7 8 9 10 11 12
package main
import"fmt"
funcmain() { type location struct{ lat, long float64 } opportunity := location{lat: -14.5684, long: 175.4722636} fmt.Println(opportunity)