当前位置:网站首页>Go language - structure
Go language - structure
2022-06-21 15:38:00 【Crying while learning】
An array can be used to store the same type of data array.
To store different types of data, you can try the structure struct.
Definition of structure
type struct_name struct {
Field name 1 Field type 1
Field name 2 Field type 2
}func main() {
// Format 1
var p1 Person_struct
p1.name = "liqi"
p1.age = 18
// Format 2
p2 := Person_struct{}
p2.name = "liqi"
p2.age = 19
// Format 3
p3 := Person_struct{name: "liqi", age: 20}
// Format 4
p4 := Person_struct{
name: "liqi",
age: 21,
}
// Format 5
p5 := Person_struct{"liqi", 22}
fmt.Printf("p1:%v \np2:%v \np3:%v \np4:%v \np5:%v", p1, p2, p3, p4, p5)
}
Structure pointer
Structure is value type data , So the copy is a deep copy .
If you want to implement shallow copy , It can be realized through structure pointer .
type Person_struct struct {
name string
age int
}
func main() {
// Format 1
fmt.Println("--- Format 1---")
var p1 Person_struct
p1.name = "liqi"
p1.age = 18
var pp1 *Person_struct
pp1 = &p1
fmt.Printf("p1 Value :%v ,p1 The type of :%T\n", p1, p1)
fmt.Printf("p1 The address of :%p\n", &p1)
fmt.Printf("pp1 Memory address stored :%p\n", pp1)
fmt.Printf("pp1 Type of storage :%T\n", pp1)
fmt.Printf("pp1 Stored real values :%v\n", *pp1)
// Format 2
fmt.Println("--- Format 2---")
pp2 := &Person_struct{name: "liqi", age: 19}
fmt.Printf("pp2 Memory address stored :%p\n", pp2)
fmt.Printf("pp2 Type of storage :%T\n", pp2)
fmt.Printf("pp2 Stored real values :%v\n", *pp2)
}
Use built-in functions new() Create pointer
new() The function is go A function used to create a week type pointer in .
type Person_struct struct {
name string
age int
}
func main() {
var p1 Person_struct
p1.name = "liqi"
p1.age = 18
pp1 := new(Person_struct)
pp1 = &p1
fmt.Printf("p1 Value :%v ,p1 The type of :%T\n", p1, p1)
fmt.Printf("p1 The address of :%p\n", &p1)
fmt.Printf("pp1 Memory address stored :%p\n", pp1)
fmt.Printf("pp1 Type of storage :%T\n", pp1)
fmt.Printf("pp1 Stored real values :%v\n", *pp1)
}
Anonymous field of structure
Anonymous structure
Since structures are generally used to store a set of types of data , However, the nomination structure can only be used once , So not often .
struct_name := struct {
Define fields
} {
Field assignment
}func main() {
p1 := struct {
name string
age int
}{
name: "John",
age: 30,
}
fmt.Println(p1)
}![]()
Anonymous field
Anonymous fields use the data type as the field name by default , therefore Type cannot be repeated .
type struct_name struct {
type1
type2
}type Pserson_struct struct {
string
int
}
func main() {
p1 := Pserson_struct{"zhangsan", 20}
fmt.Println(p1)
}![]()
Nested structure
Because the structure is a value type , So the structure is nested In general, the internal structure will use the structure pointer . Otherwise, the internal structure will be copied .
type Student struct {
name string
age int
book *Book
}
type Book struct {
bookName string
price float64
}
func main() {
book1 := Book{"Go Language from the beginning to give up ", 100.0}
student1 := Student{" Zhang San ", 18, &book1}
fmt.Println(student1)
fmt.Println(student1.book)
}
Promotion field
If a nested structure , An anonymous field is a structure , Then the fields in the structure of this anonymous field are promoted fields .
You can go directly through struct_name. Field name direct access , The name of a structure that does not require a field in the middle .
type Student struct {
name string
age int
*Book
}
type Book struct {
bookName string
price float64
}
func main() {
book1 := Book{"Go Language from the beginning to give up ", 100.0}
student1 := Student{" Zhang San ", 18, &book1}
fmt.Println(student1)
fmt.Println(student1.bookName, student1.price)
}
边栏推荐
- Connecting MySQL with C language under Windows system
- Gather high-quality ar application developers, and help the AR field prosper with technology
- Rk3399 platform development series explanation (network debugging) 7.33 network performance optimization
- Online keyboard key detection tool
- 通过编译内核的方式增加系统调用
- Select article (040) - what is the log output when you click this paragraph?
- Is it safe to open a securities account by downloading the app of qiniu school? Is there a risk?
- Integration of sparkstreaming and sparksql
- Kubeneters' CNI network plug-in installation Kube ovn
- Apple was fined by Dutch regulators, totaling about RMB 180million
猜你喜欢

Best practice | how to use Tencent cloud micro build to develop enterprise portal applications from 0 to 1

After the uproar, is the yuan universe "cool"?

Promotion guide for large enterprises: material preparation, PPT writing and on-site defense

Fluent encapsulates an immersive navigation bar with customizable styles NavigationBar

Select everything between matching brackets in vs Code - select everything between matching brackets in vs Code
![[leetcode] sum of two numbers - go language solution](/img/b8/d40cfe3efeedd816c5b81a1c939bd7.jpg)
[leetcode] sum of two numbers - go language solution

H2O brings AI master NLP technology to enterprises

Perfect partner of ebpf: cilium connected to cloud native network

Stm32l431 immediate sleep mode (code + explanation)

C language to achieve three chess (detailed explanation)
随机推荐
Connecting MySQL with C language under Windows system
For the first time in China, Tsinghua and other teams won the wsdm2022 only best paper award, and Hong Kong Chinese won the "time test Award"
Phantom star VR product details 34: Happy pitching
2020-11-12 meter skipping
Metric win computer application
Stm32l431 immediate sleep mode (code + explanation)
我不太想在網上開戶,網上股票開戶安全嗎
Integration of sparkstreaming and sparksql
Shared memory communication between processes
Finding minimum spanning tree by using union search set
进程之间使用共享内存通信
Talk about MySQL's locking rule "hard hitting MySQL series 15"
[cicadaplayer] read and write of HLS stream
‘maxflow‘ has no attribute ‘Graph‘
Warning about UUIDs in MySQL master-slave replication
Invisible characters encountered \u200b
Brain: machine learning reveals two different neuroanatomical subtypes of schizophrenia
GO语言-指针
C language selection type supplement
Niuke - real exercise-01