当前位置:网站首页>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)
}
边栏推荐
- [Development Notes] cloud app control on device based on smart cloud 4G adapter gc211
- 【MySQL 12】MySQL 8.0.18 重新初始化
- [coppeliasim4.3] C calls UR5 in the remoteapi control scenario
- Lucene skip table
- Comparison of advantages and disadvantages between most complete SQL and NoSQL
- Hisat2 - stringtie - deseq2 pipeline for bulk RNA seq
- c语言指针的概念
- Grpc message sending of vertx
- Technical dry goods | reproduce iccv2021 best paper swing transformer with Shengsi mindspire
- 项目经验分享:基于昇思MindSpore实现手写汉字识别
猜你喜欢
How long is the fastest time you can develop data API? One minute is enough for me
Lucene introduces NFA
PAT甲级 1028 List Sorting
Reconnaissance et détection d'images - Notes
PAT甲级 1030 Travel Plan
Technical dry goods Shengsi mindspire elementary course online: from basic concepts to practical operation, 1 hour to start!
Topic | synchronous asynchronous
密西根大学张阳教授受聘中国上海交通大学客座教授(图)
Application of pigeon nest principle in Lucene minshouldmatchsumscorer
技术干货|关于AI Architecture未来的一些思考
随机推荐
Jeecg menu path display problem
Industrial resilience
Qtip2 solves the problem of too many texts
FileInputStream and fileoutputstream
Partage de l'expérience du projet: mise en œuvre d'un pass optimisé pour la fusion IR de la couche mindstore
Robots protocol
Technical dry goods | hundred lines of code to write Bert, Shengsi mindspire ability reward
Shengsi mindspire is upgraded again, the ultimate innovation of deep scientific computing
Vertx's responsive redis client
Technical dry goods Shengsi mindspire innovation model EPP mvsnet high-precision and efficient 3D reconstruction
Use of file class
The difference between typescript let and VaR
技术干货|利用昇思MindSpore复现ICCV2021 Best Paper Swin Transformer
Image recognition and detection -- Notes
Use of other streams
TypeScript let与var的区别
HISAT2 - StringTie - DESeq2 pipeline 进行bulk RNA-seq
An overview of IfM Engage
Comparison of advantages and disadvantages between most complete SQL and NoSQL
截图工具Snipaste