十六.golang的结构体
结构体是用户定义的类型,表示若干个字段(Field)的集合。有时应该把数据整合在一起,而不是让这些数据没有联系。这种情况下可以使用结构体。
type Employee struct {
firstName string
lastName string
age int
}
type Employee struct {
firstName, lastName string
age, salary int
}
上面的结构体 Employee 称为 命名的结构体(Named Structure)。我们创建了名为 Employee 的新类型,而它可以用于创建 Employee 类型的结构体变量。
var employee struct {
firstName, lastName string
age int
}
package main import ( "fmt" ) type Employee struct { firstName, lastName string age, salary int } func main() { //creating structure using field names emp1 := Employee{ firstName: "Sam", age: 25, salary: 500, lastName: "Anderson", } //creating structure without using field names emp2 := Employee{"Thomas", "Paul", 29, 800} fmt.Println("Employee 1", emp1) fmt.Println("Employee 2", emp2) }
在上述程序的第 7 行,我们创建了一个命名的结构体 Employee。而在第 15 行,通过指定每个字段名的值,我们定义了结构体变量 emp1。字段名的顺序不一定要与声明结构体类型时的顺序相同。在这里,我们改变了 lastName 的位置,将其移到了末尾。这样做也不会有任何的问题。
Employee 1 {Sam Anderson 25 500}
Employee 2 {Thomas Paul 29 800}
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) }
Employee 3 {Andreah Nikola 31 5000}
当定义好的结构体并没有被显式地初始化时,该结构体的字段将默认赋为零值。
package main import ( "fmt" ) type Employee struct { firstName, lastName string age, salary int } func main() { var emp4 Employee //zero valued structure fmt.Println("Employee 4", emp4) }
该程序定义了 emp4,却没有初始化任何值。因此 firstName 和 lastName 赋值为 string 的零值("")。而 age 和 salary 赋值为 int 的零值(0)。该程序会输出:
Employee 4 { 0 0}
package main import ( "fmt" ) type Employee struct { firstName, lastName string age, salary int } func main() { emp5 := Employee{ firstName: "John", lastName: "Paul", } fmt.Println("Employee 5", emp5) }
Employee 5 {John Paul 0 0}
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) }
First Name: Sam Last Name: Anderson Age: 55 Salary: $6000
package main import ( "fmt" ) type Employee struct { firstName, lastName string age, salary int } func main() { var emp7 Employee emp7.firstName = "Jack" emp7.lastName = "Adams" fmt.Println("Employee 7:", emp7) }
Employee 7: {Jack Adams 0 0}
还可以创建指向结构体的指针。
package main import ( "fmt" ) type Employee struct { firstName, lastName string age, salary int } func main() { emp8 := &Employee{"Sam", "Anderson", 55, 6000} fmt.Println("First Name:", (*emp8).firstName) fmt.Println("Age:", (*emp8).age) }
First Name: Sam
Age: 55
package main import ( "fmt" ) type Employee struct { firstName, lastName string age, salary int } func main() { emp8 := &Employee{"Sam", "Anderson", 55, 6000} fmt.Println("First Name:", emp8.firstName) fmt.Println("Age:", emp8.age) }
First Name: Sam
Age: 55
以下代码创建一个 Person 结构体,它含有两个匿名字段 string 和 int。
type Person struct {
string
int
}
我们接下来使用匿名字段来编写一个程序。
package main import ( "fmt" ) type Person struct { string int } func main() { p := Person{"Naveen", 50} fmt.Println(p) }
在上面的程序中,结构体 Person 有两个匿名字段。p := Person{"Naveen", 50} 定义了一个 Person 类型的变量。该程序输出 {Naveen 50}。
package main import ( "fmt" ) type Person struct { string int } func main() { var p1 Person p1.string = "naveen" p1.int = 50 fmt.Println(p1) }
{naveen 50}
结构体的字段有可能也是一个结构体。这样的结构体称为嵌套结构体。
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) }
Name: Naveen Age: 50 City: Chicago State: Illinois
如果是结构体中有匿名的结构体类型字段,则该匿名结构体里的字段就称为提升字段。这是因为提升字段就像是属于外部结构体一样,可以用外部结构体直接访问。我知道这种定义很复杂,所以我们直接研究下代码来理解吧。
type Address struct {
city, state string
}
type Person struct {
name string
age int
Address
}
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) //city is promoted field fmt.Println("State:", p.state) //state is promoted field }
Name: Naveen Age: 50 City: Chicago State: Illinois
如果结构体名称以大写字母开头,则它是其他包可以访问的导出类型(Exported Type)。同样,如果结构体里的字段首字母大写,它也能被其他包访问到。
在你的 Go 工作区的 src 目录中,创建一个名为 structs 的文件夹。另外在 structs 中再创建一个目录 computer。
在 computer 目录中,在名为 spec.go 的文件中保存下面的程序。
package computer type Spec struct { //exported struct Maker string //exported field model string //unexported field Price int //exported field }
package main import "structs/computer" import "fmt" func main() { var spec computer.Spec spec.Maker = "apple" spec.Price = 50000 fmt.Println("Spec:", spec) }
包结构如下所示:
src
structs
computer
spec.go
main.go
在上述程序的第 3 行,我们导入了 computer 包。在第 8 行和第 9 行,我们访问了结构体 Spec
如果我们试图访问未导出的字段 model,编译器会报错。将 main.go 的内容替换为下面的代码。
package main import "structs/computer" import "fmt" func main() { var spec computer.Spec spec.Maker = "apple" spec.Price = 50000 spec.model = "Mac Mini" fmt.Println("Spec:", spec) }
package main import ( "fmt" ) type name struct { firstName string lastName string } 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") } }
在上面的代码中,结构体类型 name 包含两个 string
上面代码中 name1 和 name2 相等,而 name3 和 name4 不相等。该程序会输出:
name1 and name2 are equal name3 and name4 are not equal
package main import ( "fmt" ) type image struct { data map[int]int } func main() { 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") } }