当前位置:网站首页>Go structure method
Go structure method
2022-06-26 13:05:00 【thlzjfefe】
#### Go Structure method
I was so busy today that I didn't want to update the content , Later, it was completed ahead of schedule , So we still need to update ;
After all, persistence is not easy ! come on. , I believe that no matter you or me , There are some things you want to do , Then stick to it , Leave the rest to Providence ;
From yesterday to this morning, I have been dealing with wechat personal account payment ,*** It's probably that if an individual has no ability or permission to open wechat payment function ,
And want to use the payment function of wechat , That may require some special treatment , The functions of Alipay are the same as above ***;
If you are interested in personal payment , You can call me ^_^;
I would like to recommend a song here
Original singing : Zhao Fangjing
---
##### Method
In some cases, define methods : Such as the behavior of the structure , It takes a method to complete ;
Go Methods in are applied to the specified data type ( Bind to the specified data type ), therefore *** Custom types can have methods , It's not just the structure ***;
Method declaration and call :
type A struct {
Name string
}
func (a A) say(){
fmt.Println(a.Name)
}
explain :
1. func(a A) say() {} Represents a structure A There's a way , The name is say
2. (a A) reflect say Methods and A Type binding
Case study : 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
|
explain :
1. say Methods and Person Type binding
2. say Method can only pass through Person Type to call , Can't call directly , You can't use other types of variables to call ;
3. say Method and others OOP The language is very different ,*** There are no static methods ***;
4. func (p Person) say(){} p It means which Person The instance , This p It's a copy of it , Similar to function arguments ;
5. p*** Called the receiver ***, You can customize , It is generally recommended to name some types that can be intuitively experienced ;
---
Let's try another example : 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
|
explain : The above small example integrates the previous knowledge , There is also the knowledge to be learned in this section , After learning this section, you should be able to understand ; ##### The principle of method calling and parameter passing mechanism The mechanism of method calling and parameter passing is basically the same as that of function , The difference is that when a method is called , The variable that invokes the method will also be passed to the method as an argument ; If the receiver is a value type, copy the value , If the receiver is a reference type, copy the address ;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
|
Method considerations : 1. A structure is a value type , Following the value type passing mechanism in method calls ; 2. If you need to change the value of the structure variable , Similar to the above example , Handle through structure pointer ; 3. Go The method in acts on the method of the specified data type ( Bind to the specified data type ); 4. The rules of the control scope of a method are the same as those of a function , Method names start in lowercase , Can only be accessed within this package , Method initial is capitalized , It can be accessed in this package and other packages ; 5. If a structure implements String() Method , that fmt.Println() The default is to call String Method to output ; 6. When the method receiver is a value type , You can call... Directly with a pointer type variable , The reverse can also ; 7. Whether the receiver is a value type or a reference type, you can directly use the instance variable to call the method ; Case study :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
|
--- To sum up : Whatever the call form ( Whether the caller is a value type or a reference type ), It really determines whether it is value copy or address copy , It depends on the type of receiver of the method ; Such as : (p Person) Then you copy the value ,(p *Person) It's a copy of the address ; Today's content , It's a little twisted , That's all for the moment , Learning a new programming language requires discarding other programming styles , Hope to make progress together ; The latest content will be posted on the personal wechat official account , Welcome to exchange and learn together
边栏推荐
猜你喜欢

软件测试 - 基础篇

深度解析当贝盒子B3、腾讯极光5S、小米盒子4S之间的区别

Don't mess with full_ Case and parallel_ CASE

el-form-item 包含两个input, 校验这两个input

opencv高速下载
![[esp32-c3][rt-thread] run RT-Thread BSP minimum system based on esp32c3](/img/4a/503240b332e3279047c438f1d9845e.png)
[esp32-c3][rt-thread] run RT-Thread BSP minimum system based on esp32c3

Lightflow completed the compatibility certification with "daocloud Enterprise Cloud native application cloud platform"

倍福EtherCAT Xml描述文件更新和下载

首批通过!百度智能云曦灵平台获信通院数字人能力评测权威认证

EasyGBS如何解决对讲功能使用异常?
随机推荐
软件测试 - 概念篇
倍福Ethercat模块网络诊断和硬件排查的基本方法
Electron official docs series: Contributing
5月产品升级观察站
详细讲解C语言11(C语言系列)
F - Charm Bracelet
Basic principle and application routine of Beifu PLC rotary cutting
ES6模块
Processsing 鼠标交互 学习
深度解析当贝盒子B3、腾讯极光5S、小米盒子4S之间的区别
B - Bridging signals
.NET MAUI 性能提升
【网络是怎么连接的】第二章(上): 建立连接,传输数据,断开连接
[极客大挑战 2019]RCE ME 1
el-form-item 包含两个input, 校验这两个input
Electron official docs series: References
Tiger Dao VC products are officially launched, a powerful supplement to seektiger ecology
Word文档导出(使用固定模板)
G - Cow Bowling
Deep parsing MySQL binlog