结构体
结构体的初步使用
package main
import "fmt"
type DefineStruct struct {
name string
gender string
}
type PersonStruct struct {
name string
gender string
}
func main() {
StructName := DefineStruct{"Joker Xue", "Man"}
fmt.Println(StructName)
P := PersonStruct{"Joker Even", "Man"}
fmt.Println(P)
var PersonOne PersonStruct
var PersonTwo PersonStruct
PersonOne.name = "Joker Xue"
PersonOne.gender = "Man"
PersonTwo.name = "Joker Even"
PersonTwo.gender = "Man"
fmt.Printf("PersonOne'S Name: %s\n", PersonOne.name)
fmt.Printf("PersonOne'S gender: %s\n", PersonOne.gender)
fmt.Printf("PersonTwo'S Name: %s\n", PersonOne.name)
fmt.Printf("PersonTwo'S gender: %s\n",PersonTwo.gender)
}
结构体指针
package main
import "fmt"
type Books struct {
title string
author string
}
func main() {
var Book1 Books
var Book2 Books
Book1.title = "GOLANG ROADMAP"
Book1.author = "谭先生"
Book2.title = "Algorithm-Pattern"
Book2.author = "Introduction"
PrintBook(&Book1)
PrintBook(&Book2)
}
func PrintBook(book *Books) {
fmt.Printf("Book Title: %s\n", book.title)
fmt.Printf("Book Author: %s\n", book.author)
}
结构体的嵌套
结构体的匿名字段
package main
import "fmt"
type PersonStruct struct {
name string
gender bool
}
type StudentStruct struct {
PersonStruct
class string
}
func main() {
Joker := StudentStruct{PersonStruct{"Joker Even", true}, "软件20301"}
fmt.Printf("Joker'S Class: %s\n", Joker.class)
Joker.class = "人工智能21301"
fmt.Printf("Joker'S Class: %s\n", Joker.class)
}
结构体嵌套
package main
import "fmt"
type ninjaVillage struct {
where string
}
type ninja struct {
name string
grade string
address ninjaVillage
}
func main() {
var kakasasi ninja
kakasasi.name = "卡卡西"
kakasasi.grade = "火影"
kakasasi.address = ninjaVillage{
where: "木叶村",
}
fmt.Println(kakasasi)
}
导出结构体和字段
这里有些坑
请先看一下Golang项目目录结构
使用Golang命令初始化项目
在src目录下
go mod init "github.com/jokereven/golang-review"
目录结构
src
structs
computer
spec.go
main.go
在computer文件夹下创建spec.go
package computer
type Spec struct {
Maker string
}
创建main.go文件
package main
import "structs/computer"
import "fmt"
func main() {
var spec computer.Spec
spec.Maker = "apple"
fmt.Println("Spec:", spec)
}
结构体是值类型
package main
import "fmt"
type name struct {
firstName string
lastName string
}
func main() {
name1 := name{"Joker", "Even"}
name2 := name{"Joker", "Even"}
if name1 == name2 {
fmt.Println("ok")
} else {
fmt.Println("not ok")
}
name3 := name{"Joker", "Xue"}
name4 := name{}
if name3 == name4 {
fmt.Println("ok")
}else{
fmt.Println("not ok")
}
}
package main
import "fmt"
type image struct {
data map[int]int
}
func main() {
image1 := image{data: map[int]int{
0: 996,
}}
image2 := image{data: map[int]int{
1: 1024,
}}
if image1 == image2 {
fmt.Println("ok")
}
}
结构体作为函数的参数
package main
import "fmt"
type person struct {
name string
gender string
}
func main() {
var Person1 person
var Person2 person
Person1.name = "Joker"
Person2.gender = "Man"
Person2.name = "STEVE"
Person2.gender = "Man"
PrintPerson(Person1)
PrintPerson(Person2)
}
func PrintPerson(people person) {
fmt.Println("Person1 name:", people.name)
fmt.Println("Person1 gender:", people.gender)
}