当前位置:网站首页>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)
}

边栏推荐
- Analysis of the ninth Blue Bridge Cup single chip microcomputer provincial competition
- Lombok cooperates with @slf4j and logback to realize logging
- Warehouse database fields_ Summary of SQL problems in kingbase8 migration of Jincang database
- An overview of IfM Engage
- Use of other streams
- Download address collection of various versions of devaexpress
- 【LeetCode】2. Valid Parentheses·有效的括号
- opensips与对方tls sip trunk对接注意事项
- 技术干货|百行代码写BERT,昇思MindSpore能力大赏
- Realize the reuse of components with different routing parameters and monitor the changes of routing parameters
猜你喜欢
随机推荐
VMWare网络模式-桥接,Host-Only,NAT网络
Technology dry goods | luxe model for the migration of mindspore NLP model -- reading comprehension task
Leetcode 198: 打家劫舍
Technical dry goods | hundred lines of code to write Bert, Shengsi mindspire ability reward
研究显示乳腺癌细胞更容易在患者睡觉时进入血液
Lucene hnsw merge optimization
【MindSpore论文精讲】AAAI长尾问题中训练技巧的总结
Technical dry goods | alphafold/ rosettafold open source reproduction (2) - alphafold process analysis and training Construction
技术干货|昇思MindSpore可变序列长度的动态Transformer已发布!
Project experience sharing: realize an IR Fusion optimization pass of Shengsi mindspire layer
Segment read
Collector in ES (percentile / base)
Vertx's responsive MySQL template
The underlying mechanism of advertising on websites
Responsive MySQL of vertx
PAT甲级 1030 Travel Plan
Rabbit MQ message sending of vertx
Technical dry goods Shengsi mindspire operator parallel + heterogeneous parallel, enabling 32 card training 242 billion parameter model
Talk about floating
The babbage industrial policy forum









