当前位置:网站首页>Go language self-study series | golang method
Go language self-study series | golang method
2022-04-23 08:50:00 【COCOgsta】
Video source :B standing 《golang Introduction to project practice [2021 newest Go Language course , There is no nonsense , Dry only ! Ongoing update ...]》
Organize the teacher's course content and test notes while studying , And share it with you , Infringement is deleted , Thank you for your support !
Attach summary sticker :Go Language self-study series | Summary _COCOgsta The blog of -CSDN Blog
go The language has no object-oriented features , There is no concept of class objects . however , You can use structures to simulate these properties , We all know that there are concepts such as class methods in object-oriented . We can also declare some methods , Belong to a structure .
go Grammar of pre research method
Go The method in , It's a special function , Domain of definition struct above ( And struct relation 、 binding ), go by the name of struct The recipient of (receiver). Generally speaking , The method is a function with a receiver .
The syntax is as follows :
type mytype struct{}
func (recv mytype) my_method(para) retrun_type {}
func (recv *mytype) my_method(para) return_type {}
mytype: Define a structure
recv: The structure that accepts the method (receiver)
my_method: Method name
para: parameter list
return_type: return type
As can be seen from the grammatical format , A method is very similar to a function , One more acceptance type .
example
package main
import "fmt"
type Person struct {
name string
}
func (per Person) eat() {
fmt.Println(per.name + " eating....")
}
func (per Person) sleep() {
fmt.Println(per.name + " sleep....")
}
func main() {
var per Person
per.name = "tom"
per.eat()
per.sleep()
}
Running results
[Running] go run "d:\SynologyDrive\ software development \go\golang Introduction to project practice \goproject\360duote.com\pro01\test.go"
tom eating....
tom sleep....
go Precautions for language methods
- Methodical receiver type Not necessarily struct type ,type Defined type alias 、slice、map、channel、func All types are OK .
- struct The method combined with it is equivalent to the class in object-oriented . It's just struct It can be separated from its method , It doesn't have to belong to the same file , But it must belong to the same package .
- Methods have two types of reception :(T Type) and (T *Type), There is a difference between them .
- The method is the function , therefore Go There is no method overload in (overload) That's what I'm saying , That is, all method names in the same type must be unique .
- If receiver It's a pointer type , Will automatically dereference .
- Methods and type It's separate. , Means the behavior of the instance (behavior) And data storage (field) It's separate. , But they go through receiver Build relationships .
版权声明
本文为[COCOgsta]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230822135776.html
边栏推荐
- 测试你的机器学习流水线
- 2021 Li Hongyi's adaptive learning rate of machine learning
- Go语言自学系列 | golang方法
- 洋桃电子STM32物联网入门30步笔记三、CubeMX图形化编程、设置开发板上的IO口
- Wechat: get the owner of a single tag
- On time atom joins hands with oneos live broadcast, and the oneos system tutorial is fully launched
- Single chip microcomputer nixie tube stopwatch
- Star Trek强势来袭 开启元宇宙虚拟与现实的梦幻联动
- 在sqli-liabs学习SQL注入之旅(第十一关~第二十关)
- Introduction to GUI programming swing
猜你喜欢

Solidity 问题汇总

使用flask和h5搭建网站/应用的简要步骤

Virtual online exhibition - Online VR exhibition hall realizes 24h immersive exhibition viewing

Download and install bashdb

MATLAB 画五星红旗

洋桃电子STM32物联网入门30步笔记一、HAL库和标准库的区别

Idea is configured to connect to the remote database mysql, or Navicat fails to connect to the remote database (solved)
![Flash project cross domain interception and DBM database learning [Baotou cultural and creative website development]](/img/67/1f9df4236b0aac3480836d45ab8561.png)
Flash project cross domain interception and DBM database learning [Baotou cultural and creative website development]

Test your machine learning pipeline

'bully' Oracle enlarged its move again, and major enterprises deleted JDK overnight...
随机推荐
PLC point table (register address and point table definition) cracking detection scheme -- convenient for industrial Internet data acquisition
Study notes of deep learning (8)
Illegal character in scheme name at index 0:
L2-023 图着色问题 (25 分)(图的遍历)
引用传递1
在sqli-liabs学习SQL注入之旅(第十一关~第二十关)
Consensus Token:web3. 0 super entrance of ecological flow
2021李宏毅机器学习之Adaptive Learning Rate
请提前布局 Star Trek突破链游全新玩法,市场热度持续高涨
Cadence process angle simulation, Monte Carlo simulation, PSRR
洋桃电子STM32物联网入门30步笔记二、CubeIDE下载、安装、汉化、设置
tsdf +mvs
正点原子携手OneOS直播 OneOS系统教程全面上线
ESP32程序下载失败,提示超时
Single chip microcomputer nixie tube stopwatch
Valgrind and kcache grind use run analysis
OneFlow学习笔记:从Functor到OpExprInterpreter
共享办公室,提升入驻体验
Play with binary tree (25 points)
LeetCode_DFS_中等_1254. 统计封闭岛屿的数目