golang struct

ex1

/*
https://golangbot.com/structs/
struct 结构
结构就是一组字段。
*/ package main import "fmt" // 声明一个结构
type Employee struct {
firstName string
lastName string
age int
}
//// 同类型简化声明
//type Employee struct {
// firstName, lastName string
// age, salary int
//} func main() { // 匿名结构, 不给结构名字
var employee struct {
firstName, lastName string
age int
}
employee.age = 33
fmt.Println(employee) // 结构初始化为零値
fmt.Println("Employee:", Employee{})
}

ex2

// 创建命名结构
package main import (
"fmt"
) type Employee struct {
firstName, lastName string
age, salary int
} func main() { //使用字段名字创建结构
emp1 := Employee{
firstName: "Sam",
age: 25,
salary: 500,
lastName: "Anderson",
} //创建结构不使用字段名字
emp2 := Employee{"Thomas", "Paul", 29, 800} fmt.Println("Employee 1", emp1)
fmt.Println("Employee 2", emp2)
}

ex3

// 创建匿名结构
package main import (
"fmt"
) func main() {
emp3 := struct { // 这里定义
firstName, lastName string
age, salary int
}{ // 这里初始化
firstName: "Andreah",
lastName: "Nikola",
age: 31,
salary: 5000,
} fmt.Println("Employee 3", emp3)
}

ex4

// 结构字段访问
package main import (
"fmt"
) type Employee struct {
firstName, lastName string
age, salary int
} func main() {
emp6 := Employee{"Sam", "Anderson", 55, 6000}
fmt.Println("First Name:", emp6.firstName)
fmt.Println("Last Name:", emp6.lastName)
fmt.Println("Age:", emp6.age)
fmt.Printf("Salary: $%d", emp6.salary)
fmt.Println()
var emp7 Employee
emp7.firstName = "Jack"
emp7.lastName = "Adams"
fmt.Println("Employee 7:", emp7)
}

ex5

// 结构指针
package main import (
"fmt"
) type Employee struct {
firstName, lastName string
age, salary int
} func main() {
// 这里emp8就是一个结构指针
emp8 := &Employee{"Sam", "Anderson", 55, 6000}
fmt.Println("First Name:", (*emp8).firstName) // 通过指针访问结构字段
fmt.Println("Age:", (*emp8).age) // golang提供另一种可选方法来访问结构字段
fmt.Println("First Name:", emp8.firstName)
fmt.Println("Age:", emp8.age)
}

ex6

// 结构匿名字段
package main import (
"fmt"
) // 定义的结构, 包含有匿名字段
type Person struct {
string
int
} func main() {
// 带匿名字段的结构的实例化
p1 := Person{"Naveen", 50}
fmt.Println(p1)
// 尽管匿名字段没有名字, 它的缺省名字是类型名字
p1.string = "naveen_new"
p1.int = 100
fmt.Println(p1)
}

ex7

// 结构嵌套
package main import (
"fmt"
) type Address struct {
city, state string
}
type Person struct {
name string
age int
address Address
} func main() {
var p Person
p.name = "Naveen"
p.age = 50
p.address = Address {
city: "Chicago",
state: "Illinois",
}
fmt.Println("Name:", p.name)
fmt.Println("Age:",p.age)
fmt.Println("City:",p.address.city)
fmt.Println("State:",p.address.state)
}

ex8

// 结构提升字段
package main import (
"fmt"
) type Address struct {
city, state string
}
type Person struct {
name string
age int
Address // 当结构中, 存在一个匿名的结构型字段。 我们把这个字段叫提升字段
} func main() {
var p Person
p.name = "Naveen"
p.age = 50
p.Address = Address{
city: "Chicago",
state: "Illinois",
}
fmt.Println("Name:", p.name)
fmt.Println("Age:", p.age)
fmt.Println("City:", p.city) //这个字段,直接提升到上一级,可以直接访问
fmt.Println("State:", p.state) //这个字段,直接提升到上一级,可以直接访问
fmt.Println("City:", p.Address.city)
fmt.Println("State:", p.Address.state)
}

ex9

// 结构导出<即外部可访问性>
package main import (
"fmt"
) // 当前结构首字母大写, 则外部包可以访问
type Address struct {
// 字段同理
City, state string
}
type Person struct {
name string
age int
Address // 当结构中, 存在一个匿名的结构型字段。 我们把这个字段叫提升字段
} func main() {
var p Person
p.name = "Naveen"
p.age = 50
p.Address = Address{
City: "Chicago",
state: "Illinois",
}
fmt.Println(p) }

ex10

// 结构相等
// 结构是值类型,可以直接比较(仅当他们字段可以比较时)
package main import (
"fmt"
) type name struct {
firstName string
lastName string
} type image struct {
data map[int]int // 这个字段是不可比较的
} func main() {
name1 := name{"Steve", "Jobs"}
name2 := name{"Steve", "Jobs"}
if name1 == name2 {
fmt.Println("name1 and name2 are equal")
} else {
fmt.Println("name1 and name2 are not equal")
} name3 := name{firstName:"Steve", lastName:"Jobs"}
name4 := name{}
name4.firstName = "Steve"
if name3 == name4 {
fmt.Println("name3 and name4 are equal")
} else {
fmt.Println("name3 and name4 are not equal")
} // 不可比较情况
//image1 := image{data: map[int]int{
// 0: 155,
//}}
//image2 := image{data: map[int]int{
// 0: 155,
//}}
//// 这里直接报错
//if image1 == image2 {
// fmt.Println("image1 and image2 are equal")
//}
}
上一篇:Golang Struct 声明和使用


下一篇:Go语言高级特性总结——Struct、Map与JSON之间的转化