当前位置:网站首页>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
边栏推荐
- LLVM之父Chris Lattner:编译器的黄金时代
- Single chip microcomputer nixie tube stopwatch
- Consensus Token:web3.0生态流量的超级入口
- Talent Plan 学习营初体验:交流+坚持 开源协作课程学习的不二路径
- Latex paper typesetting operation
- 在sqli-liabs学习SQL注入之旅(第十一关~第二十关)
- Stm32f103zet6 [development of standard library functions] - Introduction to library functions
- STM32F103ZET6【标准库函数开发】----库函数介绍
- LaTeX数学公式
- 论文阅读《Multi-View Depth Estimation by Fusing Single-View Depth Probability with Multi-View Geometry》
猜你喜欢

PLC point table (register address and point table definition) cracking detection scheme -- convenient for industrial Internet data acquisition

L2-022 rearrange linked list (25 points) (map + structure simulation)

Notes d'apprentissage oneflow: de functor à opexprinterpreter

Valgrind et kcachegrind utilisent l'analyse d'exécution

Bk3633 specification

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

Study notes of deep learning (8)

L2-3 浪漫侧影 (25 分)

RCC introduction of Hal Library

Redis Desktop Manager for Mac(Redis可视化工具)
随机推荐
LeetCode396.旋转数组
L2-3 浪漫侧影 (25 分)
测试你的机器学习流水线
请提前布局 Star Trek突破链游全新玩法,市场热度持续高涨
Enterprise wechat application authorization / silent login
uni-app和微信小程序中的getCurrentPages()
Test your machine learning pipeline
在sqli-liabs学习SQL注入之旅(第十一关~第二十关)
LeetCode_DFS_中等_1254. 统计封闭岛屿的数目
使用flask和h5搭建网站/应用的简要步骤
Notes d'apprentissage oneflow: de functor à opexprinterpreter
经典题目刷一刷
Judgment on heap (25 points) two insertion methods
玩转二叉树 (25 分)
Reference passing 1
是否同一棵二叉搜索树 (25 分)
Yangtao electronic STM32 Internet of things entry 30 step notes II. Cube ide download, installation, sinicization and setting
Go语言自学系列 | golang结构体指针
Notes on 30 steps of introduction to Internet of things of yangtao electronics STM32 III. Explanation of new cubeide project and setting
Kubernetes如何使用harbor拉去私有镜像