当前位置:网站首页>Go language foundation ----- 06 ----- anonymous fields, fields with the same name
Go language foundation ----- 06 ----- anonymous fields, fields with the same name
2022-07-03 07:37:00 【Mango sauce】
One Anonymous field 、 Fields with the same name
1 The function and initialization of anonymous fields
- 1)go Anonymous fields in are only types , Isn't there a name .
- 2) The role of anonymous fields : because go No packaging 、 Inherit 、 The concept of polymorphism , however go It can also be object-oriented programming , Because there are anonymous fields . For example, the following ,Student Inherited through anonymous fields Persion All members of .
Actually went to school C Of all know , It's just a simple structure nesting , But in go It's so tall .
type Persion struct {
name string
sex byte
age int
}
// The declarations of ordinary variables are var keyword , There is no need for var
type Student struct {
Persion // Anonymous field , Only type , Isn't there a name . It inherited Persion All members of
id int
addr string
}
- 3) Initialization of anonymous fields
package main
import "fmt"
type Persion struct {
name string
sex byte
age int
}
// The declarations of ordinary variables are var keyword , There is no need for var
type Student struct {
Persion // Anonymous field , Only type , Isn't there a name . It inherited Persion All members of
id int
addr string
}
func main(){
// 1. Sequence initialization
var s1 Student = Student{
Persion{
"hh", 'w',24}, 1, "sz"}
fmt.Println("s1 = ", s1)
// 2. Automatic derivation type
s2 := Student{
Persion{
"cc", 'w',24}, 2, "sz"}
fmt.Printf("s2 = %+v\n", s2)// %+v Indicates that the display is more detailed
// 3. Initialize members
s3 := Student{
id:1}
fmt.Printf("s3 = %+v\n", s3)
// When assigning values to members , The corresponding field name must be manually added before each value , Otherwise the newspaper mixture of field:value and value initializers
s4 := Student{
Persion: Persion{
name: "ll", sex: 'w'}, id: 1}
//s4 := Student{Persion{"cc", 'w'}, 2}// err:mixture of field:value and value initializers
fmt.Printf("s4 = %+v\n", s4)
// 4. Use member operations to specify member initialization
var s5 Student
s5.Persion.name = "qq" // Write at this time s5.name It's the same
s5.name = "mm"
s5.age = 24
s5.id = 1
fmt.Printf("s5 = %+v\n", s5)
}
result :
2 Fields with the same name
1) A field with the same name has a member in an anonymous field , There is also a member with the same name outside the anonymous field .
type Persion struct {
name string
sex byte
age int
}
// The declarations of ordinary variables are var keyword , There is no need for var
type Student struct {
Persion // Anonymous field , Only type , Isn't there a name . It inherited Persion All members of
id int
addr string
name string // Fields with the same name , because Persion There's one in there, too name. The compiler operates according to the principle of proximity , That is, this member has high priority .
}
- 2) When there is a field with the same name , The compiler will search according to the principle of proximity . If found in this scope , Direct use , If not, go to the inherited structure to find .
package main
import "fmt"
type Persion struct {
name string
sex byte
age int
}
// The declarations of ordinary variables are var keyword , There is no need for var
type Student struct {
Persion // Anonymous field , Only type , Isn't there a name . It inherited Persion All members of
id int
addr string
name string // Fields with the same name , because Persion There's one in there, too name. The compiler operates according to the principle of proximity , That is, this member has high priority .
}
func main(){
var s1 Student
s1.name = "hh" // The operation is Student Of name, instead of Persion Of name
s1.id = 1
s1.addr = "sz"
s1.Persion.name = "perName" // Display operation is required
s1.Persion.age = 22
s1.Persion.sex = 'w' // ascii Code value print character type
fmt.Printf("s1 = %+v\n", s1)
}

3 Unstructured type anonymous field
- 1) In fact, unstructured type anonymous fields and 1 The anonymous fields mentioned above are the same , But these types are not structures , It's usually the basic data type , for example int、short these .
Here is just to impress everyone .
package main
import "fmt"
type Persion struct {
name string
sex byte
age int
}
// The declarations of ordinary variables are var keyword , There is no need for var
type Student struct {
Persion // Anonymous field , Only type , Isn't there a name . It inherited Persion All members of
int
// string // Cannot have anonymous fields of the same type , Will report a mistake error: duplicate field string
string // Fields with the same name , because Persion There's one in there, too name. The compiler operates according to the principle of proximity , That is, this member has high priority .
}
func main(){
var s1 Student
s1 = Student{
Persion{
"h", 'w', 24}, 111, "hhh"}
fmt.Println("s1 = ", s1)
}

4 Structure pointer anonymous field
- 1) Empathy , Structure pointer anonymous field and 1 It's the same as what I said , Here is just to impress everyone , Specify how to use .
package main
import "fmt"
type Persion struct {
name string
sex byte
age int
}
// The declarations of ordinary variables are var keyword , There is no need for var
type Student struct {
*Persion // Anonymous field , Only type , Isn't there a name . It inherited Persion All members of
id int
addr string
}
func main(){
// 1. Don't use new The way
s1 := Student{
&Persion{
"hh", 'w', 24}, 1, "sz"}
fmt.Println(s1.name, s1.sex, s1.age, s1.id, s1.addr)
// 2. Use new The way
var s2 Student
s2.Persion = new(Persion)
s2.name = "cc"
s2.sex = 'w'
s2.age = 24
s2.id= 1
s2.addr = "sz"
fmt.Println(s2.name, s2.sex, s2.age, s2.id, s2.addr)
}

边栏推荐
- Leetcode 198: 打家劫舍
- Analysis of the problems of the 10th Blue Bridge Cup single chip microcomputer provincial competition
- Topic | synchronous asynchronous
- VMware virtual machine installation
- Es writing fragment process
- 項目經驗分享:實現一個昇思MindSpore 圖層 IR 融合優化 pass
- Epoll related references
- Common methods of file class
- TreeMap
- Qtip2 solves the problem of too many texts
猜你喜欢

Hnsw introduction and some reference articles in lucene9

Technical dry goods | reproduce iccv2021 best paper swing transformer with Shengsi mindspire

PAT甲级 1032 Sharing

Spa single page application

Use of generics

【开发笔记】基于机智云4G转接板GC211的设备上云APP控制

The embodiment of generics in inheritance and wildcards

Application of pigeon nest principle in Lucene minshouldmatchsumscorer

VMware network mode - bridge, host only, NAT network

Project experience sharing: realize an IR Fusion optimization pass of Shengsi mindspire layer
随机推荐
技术干货|昇思MindSpore算子并行+异构并行,使能32卡训练2420亿参数模型
Jeecg data button permission settings
项目经验分享:基于昇思MindSpore实现手写汉字识别
Use of file class
c语言指针的概念
pgAdmin 4 v6.11 发布,PostgreSQL 开源图形化管理工具
Read config configuration file of vertx
Implementation of breadth first in aggregation in ES
Various postures of CS without online line
Web router of vertx
VMware network mode - bridge, host only, NAT network
Spa single page application
Hnsw introduction and some reference articles in lucene9
Unified handling and interception of exception exceptions of vertx
Common architectures of IO streams
技术干货|昇思MindSpore NLP模型迁移之LUKE模型——阅读理解任务
Responsive MySQL of vertx
技术干货|利用昇思MindSpore复现ICCV2021 Best Paper Swin Transformer
技术干货 | AlphaFold/ RoseTTAFold开源复现(2)—AlphaFold流程分析和训练构建
Custom generic structure