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

边栏推荐
- Common architectures of IO streams
- Vertx metric Prometheus monitoring indicators
- Why is data service the direction of the next generation data center?
- Hello world of vertx
- C WinForm framework
- Technical dry goods Shengsi mindspire operator parallel + heterogeneous parallel, enabling 32 card training 242 billion parameter model
- 技术干货|百行代码写BERT,昇思MindSpore能力大赏
- 項目經驗分享:實現一個昇思MindSpore 圖層 IR 融合優化 pass
- Spa single page application
- JUnit unit test of vertx
猜你喜欢
![PdfWriter. GetInstance throws system Nullreferenceexception [en] pdfwriter GetInstance throws System. NullRef](/img/65/1f28071fc15e76abb37f1b128e1d90.jpg)
PdfWriter. GetInstance throws system Nullreferenceexception [en] pdfwriter GetInstance throws System. NullRef

技术干货|昇思MindSpore NLP模型迁移之Bert模型—文本匹配任务(二):训练和评估

PAT甲级 1027 Colors in Mars

URL programming

Project experience sharing: realize an IR Fusion optimization pass of Shengsi mindspire layer

Use of other streams

技术干货|AI框架动静态图统一的思考

IPv4 address

Robots protocol

PAT甲级 1032 Sharing
随机推荐
Why is data service the direction of the next generation data center?
Rabbit MQ message sending of vertx
JS monitors empty objects and empty references
Circuit, packet and message exchange
Jeecg request URL signature
【MySQL 13】安装MySQL后第一次修改密码,可以可跳过MySQL密码验证进行登录
The concept of C language pointer
Technical dry goods Shengsi mindspire innovation model EPP mvsnet high-precision and efficient 3D reconstruction
Pgadmin 4 v6.11 release, PostgreSQL open source graphical management tool
An overview of IfM Engage
昇思MindSpore再升级,深度科学计算的极致创新
Epoll related references
Lombok cooperates with @slf4j and logback to realize logging
Introduction of transformation flow
【MySQL 12】MySQL 8.0.18 重新初始化
Warehouse database fields_ Summary of SQL problems in kingbase8 migration of Jincang database
Margin left: -100% understanding in the Grail layout
Technology dry goods | luxe model for the migration of mindspore NLP model -- reading comprehension task
不出网上线CS的各种姿势
Analysis of the ninth Blue Bridge Cup single chip microcomputer provincial competition