当前位置:网站首页>Go-12-Structure
Go-12-Structure
2022-08-09 01:33:00 【hahyyy】
Structure
Arrays in Go can store the same type of data, but in structures we can define different data types for different items
A structure is a collection of data consisting of a series of data of the same or different types
The structure represents a record, similar to the class in other languages, you can define multiple fields in the structure, implement methods, instantiation, etc. for the structure
Define Structure
Structure definitions require the use of type and struct statements
The struct statement defines a new data type with one or more members
The type statement sets the name of the structure
Define a structure Student and add name and age fields for Student
type Student struct {name stringage int}Add method to struct
Unlike other object-oriented languages, Go can write classes and then write methods in the class, but it is also very clever to achieve this effect
We just need to add a receiver in front of the normal function, so that the compiler knows which struct the function belongs to
Syntax format: func (r ReceiverType) funcName(parameters) (results)
Example: Implementing the hello method for Student
func (stu *Student) hello(person string) string {return fmt.Sprintf("hello %s, I am %s", person, stu.name)}To put it into perspective, all fields of the ReceiverType type and the method funcName can be used, and it can be considered that funcName belongs to the ReceiverType
The difference between the implementation method and the implementation function is that between the func and the function name hello, plus the instance name stu and its type *Student for the method, the field name and other methods of the instance can be accessed through the instance name
method attribute
- There are no classes in Go, but there are still methods
- Combines with a type by displaying the description receiver
- Methods can only be defined for types in the same package
- Receiver can be a value or pointer of type
- There is no method overloading
- Methods can be called with values or pointers, the compiler will automatically complete the conversion
- In a sense, methods are syntactic sugar for functions, because the receiver is actually the first parameter received by the method
- If there is a method with the same name in the external structure and the embedded structure, the method of the external structure will be called first
- Type aliases do not have methods attached to the underlying type
- Methods can call non-public fields in the structure
Using structs
The structure can contain multiple data types, and the array can only be a single type of data collection
// create an instancestu := &Student{name: "Tom",}// call instance methodmsg := stu.hello("Jack")fmt.Println(msg) // hello Jack, I am Tom// instantiate with Newstu2 := new(Student)stu2.name = "Cali"// hello Alice, I am , name is given default value ""fmt.Println(stu2.hello("Alice")) struct properties
- structs in Go are very similar to structs in C, and Go has no classes
- Use type
struct{} to define structures, names follow visibility rules - Supports pointer-to-self type members
- Supports anonymous structures, which can be used as members or define member variables
- Anonymous structures can also be used for map values
- structs can be initialized with literals
- Allows to read and write structure members directly through pointers
- Same and similar members can be directly copied and assigned
- The == and != comparison operators are supported, but not > or <
- Supports anonymous fields, which essentially define fields named after a certain type
- Embedding structs as anonymous fields looks like inheritance, but not inheritance
- Anonymous field pointers can be used
Anonymous structure
Anonymous structure, structure without name
func main() {// anonymous struct, a struct without a namea := struct {Name stringAge int}{Name: "Corwien",Age: 20,}fmt.Println(a)}Anonymous structure nesting
type person struct{Name stringAge intContact struct {Phone, City stringCode int // house number}}func main() {a := person{Name:"Corwien", Age:15}a.Contact.Phone = "10086"a.Contact.City = "Guangzhou"a.Contact.Code = 2007fmt.Println(a)}Anonymous fields
Anonymous fields: structs do not have fields that name struct properties, only types
Anonymous fields must strictly follow the order of field type declaration
type person struct{stringint}func main() {// Anonymous fields must strictly follow the order of field type declarationsa := person{"Corwien", 12}fmt.Println(a)}Structure type comparison
type person struct{Name stringAge int}func main() {// Anonymous fields must strictly follow the order of field type declarationsa := person{Name:"Corwien", Age:12}b := person{Name:"Corwien", Age:12}fmt.Println(a == b)}边栏推荐
猜你喜欢

typescript89-展示任务列表功能

4-1 Matplotlib库 数据分析常用图

数据恢复软件EasyRecovery支持恢复所有类型的文件

Grid布局介绍

深度学习模型的两种部署:ONNX与Caffe

轻量级学习网络--ShuffleNet v1:Group conv的改进与channel shuffle的提出

右键新建缺少word、excel选项问题处理

JSON basics, transfer JSON data, and introduce four mainstream frameworks, jackson, gson, fastjson, and json-lib!

5-2 Seaborn 分类绘图

How SEMRush finds keywords for advertising
随机推荐
容器运维平台的故障处理-1
4-7 Matplotlib库 箱线图
右键新建缺少word、excel选项问题处理
浅谈自定义应用层协议与UDP的报文结构和注意事项
4-10 Matplotlib 多图布局
ABAP 报表中如何以二进制方式上传本地文件
Introduction to LVGL (based on v8.1-8.2)
日文翻译-在线免费日文翻译软件
【Fiddler】Fiddler实现mock测试(模拟接口数据)
Image denoising based on edge enhancement Diffusion 】 (cEED) and Coherence Enhancing coursing together (cCED) filter to realize image denoising matlab code
有相同字符串的查找
2022年中国全民健身发展白皮书
全文翻译:欧盟第29条数据保护工作组 数据保护官指南
[机缘参悟-65]:《兵者,诡道也》-6-孙子兵法解读-并战计
[Signal denoising] Based on Sage-Husa adaptive Kalman filter to realize the suppression of ocean wave magnetic field noise and the generation of ocean wave magnetic field noise with matlab code
ONNX是什么?怎么用?[简明解读版]
LeetCode精选200道--字符串篇
LeetCode精选200道--双指针篇
Wireshark packet capture tool
如何在群晖系统中安装cpolar(群晖6.X版)