当前位置:网站首页>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)}
边栏推荐
- 智能视频监控设计摄像头部分
- KQL和Lucene的区别
- 在 ASP.NET Core 中上传文件
- 《LC刷题总结》—— 二叉树
- A double non-programmer interviewed Ant, Meituan, Ctrip and other big companies with offers to share the interview process
- Latex示例参考
- Docker redis master-slave replication setup, the container cannot be started?
- 软件测试的调用接口怎么调用,逻辑是什么?
- jetson nano 开机闪一下然后黑屏
- 德语翻译器在线翻译中文
猜你喜欢
随机推荐
轻量级学习网络--ShuffleNet v1:Group conv的改进与channel shuffle的提出
任务五 处理连续型数据
在实际工作中如何开展性能测试?
JDBC技术(二)——设置通用的sql和配置文件
在 ASP.NET Core 中上传文件
How to install ngrok in Synology system (Synology 6.X version)
面试秘籍 | 软件测试必备的mysql数据库技术
智能视频监控设计摄像头部分
生成一系列随机字符串的文件
Go-7-RESTful API的设计
JSON basics, transfer JSON data, and introduce four mainstream frameworks, jackson, gson, fastjson, and json-lib!
TCP/IP协议栈
DataNode重启
Node.js:MySQL.js的基本操作增删改查
A double non-programmer interviewed Ant, Meituan, Ctrip and other big companies with offers to share the interview process
RS&FSW测试脚本
入门数据库Days6
typescript90-使用类型文件声明类型
ffplay播放控制
LeetCode每日两题02:轮转数组 (均1200道)