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

边栏推荐
- Various postures of CS without online line
- Analysis of the eighth Blue Bridge Cup single chip microcomputer provincial competition
- 技术干货|昇思MindSpore创新模型EPP-MVSNet-高精高效的三维重建
- Technology dry goods | luxe model for the migration of mindspore NLP model -- reading comprehension task
- 【LeetCode】2. Valid Parentheses·有效的括号
- PdfWriter. GetInstance throws system Nullreferenceexception [en] pdfwriter GetInstance throws System. NullRef
- Es writing fragment process
- 哪一刻你才发现青春结束了
- Use of generics
- List exercises after class
猜你喜欢

Circuit, packet and message exchange

Understanding of class

Image recognition and detection -- Notes

Homology policy / cross domain and cross domain solutions /web security attacks CSRF and XSS

Leetcode 213: looting II

c语言指针的概念

昇思MindSpore再升级,深度科学计算的极致创新

Analysis of the problems of the 12th Blue Bridge Cup single chip microcomputer provincial competition

项目经验分享:基于昇思MindSpore实现手写汉字识别

项目经验分享:基于昇思MindSpore,使用DFCNN和CTC损失函数的声学模型实现
随机推荐
UA camouflage, get and post in requests carry parameters to obtain JSON format content
Jeecg data button permission settings
HarmonyOS第三次培训笔记
技术干货|昇思MindSpore算子并行+异构并行,使能32卡训练2420亿参数模型
你开发数据API最快多长时间?我1分钟就足够了
【LeetCode】4. Best Time to Buy and Sell Stock·股票买卖最佳时机
An overview of IfM Engage
Application of pigeon nest principle in Lucene minshouldmatchsumscorer
The embodiment of generics in inheritance and wildcards
Dora (discover offer request recognition) process of obtaining IP address
TCP cumulative acknowledgement and window value update
Technical dry goods Shengsi mindspire innovation model EPP mvsnet high-precision and efficient 3D reconstruction
Map interface and method
Leetcode 198: 打家劫舍
Visit Google homepage to display this page, which cannot be displayed
c语言指针的概念
Use of other streams
Analysis of the problems of the 7th Blue Bridge Cup single chip microcomputer provincial competition
[Development Notes] cloud app control on device based on smart cloud 4G adapter gc211
Lucene hnsw merge optimization