当前位置:网站首页>Go language - Method
Go language - Method
2022-06-21 15:38:00 【Crying while learning】
What is the method ?
stay go In language , The method is a function that contains the receiver . The receiver can be a named type 、 Structure 、 The pointer .
The grammar of the method
The definition of method is almost the same as that of fir tree . Just define the receiver type before the method name .
func (t type) methodName(parameter list)(return list){
...
}type Worker struct {
name string
age int
}
func (w Worker) work() {
fmt.Println(w.name, " My job is yard farmer ")
}
func main() {
worker1 := Worker{" Zhang San ", 20}
worker1.work()
}![]()
Inherit
GO In the structure of language , If an anonymous field implements a method , The structure containing the anonymous field can also call this method .
The anonymous fields mentioned above are also generally structures , It can be understood as a parent class .( Anonymous field in subclass structure , Parent class )
go Language implements inheritance in object-oriented
// Person Structure -- Parent class
type Person struct {
name string
age int
}
// Worker Structure -- Subclass
type Worker struct {
workName string
*Person // Anonymous field , It's a structure .( A similar subclass inherits the parent class )
}
func (p Person) eat() {
fmt.Println(p.name, " I am eating ...")
}
func main() {
p1 := Person{" Zhang San ", 20}
w1 := Worker{" Code the agriculture ", &p1}
fmt.Printf("%v My occupation is :%v\n", w1.name, w1.workName)
w1.eat() // Inherited from parent class eat() Method
}
边栏推荐
- Redis introduction and Practice (with source code)
- 2022 Hunan top eight (Safety Officer) simulated test question bank and answers
- 原生JS路由,iframe框架
- Clickhouse cluster installation has too many dry goods
- Shell uses arrays
- Select article (040) - what is the log output when you click this paragraph?
- There is a PPP protocol between routers. How can there be an ARP broadcast protocol
- 57 common mistakes in golang development
- Three disciplines of elastic design, how to make the stability KPI high?
- Merge two ordered linked lists
猜你喜欢
Counter attack of flour dregs: MySQL 66 questions, 20000 words + 50 pictures!

Promotion guide for large enterprises: material preparation, PPT writing and on-site defense
![Analysis of China's social financing scale and financing structure in 2021: RMB loans to the real economy account for more than 60%[figure]](/img/93/218ba91dc6e9866e186c004ac18e0a.jpg)
Analysis of China's social financing scale and financing structure in 2021: RMB loans to the real economy account for more than 60%[figure]

Phantom star VR product details 34: Happy pitching

Tomb. Weekly update of Finance (February 14-20)

H2O brings AI master NLP technology to enterprises

2022awe opened in March, and Hisense conference tablet was shortlisted for the review of EPLAN Award

ABP Framework 5.3.0 版本新增功能和变更说明
![[Yugong series] February 2022 wechat applet -app Debug JSON configuration attribute](/img/33/103a207dc085c431557981b3c9a1d5.jpg)
[Yugong series] February 2022 wechat applet -app Debug JSON configuration attribute

Retrieve the compressed package password
随机推荐
Live long to see the true chapter
泰山OFFICE技术讲座:域在MODEL的存储结构
[go] time package
GO语言-指针
SAP QM qs41 attempts to maintain a code group with a catalog of 3 and reports an error -you need to maintain catalog 3
Warning about UUIDs in MySQL master-slave replication
Work assessment of contract law of Jilin University in March of the 22nd spring -00100
启牛学堂app下载证券开户,是安全的吗?有风险嘛?
Stm32l431 immediate sleep mode (code + explanation)
‘maxflow‘ has no attribute ‘Graph‘
GO语言-方法
Kubernetes deployment language
[leetcode] sum of two numbers - go language solution
Shared memory communication between processes
Daily practice (23): the first character that appears only once
Dentron: ultra fast open source notes in vscode
2022 Fujian latest fire protection facility operator simulation test question bank and answers
Someone is storing credit card data - how do they do it- Somebody is storing credit card data - how are they doing it?
Analysis on the scale and market structure of China's bill financing industry in 2020 [figure]
Defcampctf2122 Forensics