当前位置:网站首页>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)}
边栏推荐
猜你喜欢
SEMRush如何寻找关键词用于投放广告
椭圆曲线复习
JDBC技术(三)——使用Druid数据库连接池测试
2022PMP项目管理认证考试报考指南(1)
论文笔记:SAITS: SELF-ATTENTION-BASED IMPUTATION FOR TIMESERIES
How SEMRush finds keywords for advertising
『Another Redis DeskTop Manager』用了这款Redis可视化工具,分析效率提升12倍
OpenSceneGraph3.5.1编译
The Best Open Source Web Application Firewall to Protect Your Web Applications
VOIP使用单端口替换动态端口池进行UDP通信
随机推荐
任务五 处理连续型数据
class path resource [bean.xml] cannot be opened because it does not 错误解决方案
String compression
4-4 Matplotlib库 直方图
基于机器学习之模型树短期负荷预测(Matlab代码实现)
《LC刷题总结》—— 二叉树
typescript90-使用类型文件声明类型
Go-9-数据类型-函数
[深入研究4G/5G/6G专题-55]: L3信令控制-4-软件功能与流程的切分-CU网元的信令
在实际工作中如何开展性能测试?
torchversion.transforms的使用
考研人总结的时间管理7大忌,你中了几条?
Mysql高级篇(逻辑架构和存储引擎)
LeetCode精选200道--字符串篇
Go-8-Gin框架
字符串压缩
如何仿造一个websocket请求?
momerymap mmap 存储映射I/O
js文件的处理
谷歌翻译下载-免费谷歌翻译软件下载