当前位置:网站首页>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
边栏推荐
- idea底栏打开services
- 【IndexOf】【lastIndexOf】【split】【substring】用法详解
- Use include in databinding
- Yangtao electronic STM32 Internet of things entry 30 step notes IV. engineering compilation and download
- 应纳税所得额
- Flink reads MySQL and PgSQL at the same time, and the program will get stuck without logs
- Whether the same binary search tree (25 points)
- L2-3 浪漫侧影 (25 分)
- Technological innovation in government affairs in the construction of Digital Government
- 玩转二叉树 (25 分)
猜你喜欢

测试你的机器学习流水线

Harbor企业级镜像管理系统实战

Redis Desktop Manager for Mac(Redis可视化工具)

Notes on 30 steps of introduction to Internet of things of yangtao electronics STM32 III. Explanation of new cubeide project and setting

JVM工具之Arthas使用

ESP32程序下载失败,提示超时

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

Study notes of deep learning (8)

1099 establish binary search tree (30 points)

Star Trek's strong attack opens the dream linkage between metacosmic virtual reality
随机推荐
Summary of solid problems
MATLAB 画五星红旗
Latex paper typesetting operation
Play with binary tree (25 points)
Multi view depth estimation by fusing single view depth probability with multi view geometry
MySQL查询两张表属性值非重复的数据
Output first order traversal according to second order and middle order traversal (25 points)
valgrind和kcachegrind使用運行分析
Idea is configured to connect to the remote database mysql, or Navicat fails to connect to the remote database (solved)
Yangtao electronic STM32 Internet of things entry 30 step notes IV. engineering compilation and download
Solidity 问题汇总
PLC point table (register address and point table definition) cracking detection scheme -- convenient for industrial Internet data acquisition
L2-3 romantic silhouette (25 points)
请问中衍期货安全靠谱吗?
数字政府建设中政务中台中的技术创新点
The K neighbors of each sample are obtained by packet switching
Cadence process angle simulation, Monte Carlo simulation, PSRR
RCC introduction of Hal Library
Illegal character in scheme name at index 0:
tsdf +mvs