- 1.命令结构声明
type Employee struct {
firstName string
lastName string
age int
}
- 2.相同类型的字段可以写在一行
type Employee struct {
firstName, lastName string
age, salary int
}
- 3.匿名结构体声明
var employee struct {
firstName, lastName string
age int
}
- 4.实例化命令结构体
emp1 := Employee{
firstName: "Sam",
age: 25,
salary: 500,
lastName: "Anderson",
}
emp2 := Employee{"Thomas", "Paul", 29, 800}
- 5.匿名结构体创建
emp3 := struct {
firstName, lastName string
age, salary int
}{
firstName: "Andreah",
lastName: "Nikola",
age: 31,
salary: 5000,
}
- 6.结构体的零值
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)
- 7.结构体指针
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)
}
emp8 是结构体的地址 emp8 取得就是结构体的值
Go 语言允许我们在访问 firstName 字段时,可以使用 emp8.firstName 来代替显式的解引用 (emp8).firstName。
- 8.匿名字段
type Person struct {
string
int
}
code
package main
import (
"fmt"
)
type Person struct {
string
int
}
func main() {
p := Person{"Naveen", 50}
fmt.Println(p)
}
如何访问匿名字段?
类型名默认就是字段名
func main() {
var p1 Person
p1.string = "naveen"
p1.int = 50
fmt.Println(p1)
}
- 9.嵌套结构体
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)
}
- 10.提升字段
type Address struct {
city, state string
}
type Person struct {
name string
age int
Address
}
person 中的Address 其实就是匿名结构体,结构体 Address 有 city 和 state 两个字段,访问这两个字段就像在 Person 里直接声明的一样,因此我们称之为提升字段
下面看一下如何访问吧
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
}
- 11.导出结构体和字段
如果结构体名称以大写字母开头,则它是其他包可以访问的导出类型(Exported Type)。同样,如果结构体里的字段首字母大写,它也能被其他包访问到
结构体名称首字母和字段大小写,对同一个包的读写不受任何影响,如果不在同一个包,就有严格的显示,大写能方位,小写不能方位
- 12.结构体相等性
结构体是值类型。如果它的每一个字段都是可比较的,则该结构体也是可比较的。如果两个结构体变量的对应字段相等,则这两个变量也是相等的。
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")
}
}
如果结构体包含两个不可比较的字段,那么结构体也不可比较
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")
}
}
如何让结构体不出现零值
package employee
// 创建一个私有的结构体
type employee struct {
name string
age int
}
// 返回结构体类型
func New(name string,age int)employee{
return employee{
name:name,
age:age,
}
}
我们看使用
package main
import (
"awesomeProject/main/employee"
"fmt"
)
func main() {
var emp = employee.New("酷走",12)
fmt.Println(emp)
}
通过以上方式创建的结构体,就避免出现空值
网友评论