- Hands-On Full Stack Development with Go
- Mina Andrawos
- 138字
- 2021-07-02 12:33:31
Methods
A method is basically a function that you attach to a type. For example, let's assume we have a struct type called Person:
type Person struct{
name string
age int
}
Go allows us to attach a method to that type like this:
func (p Person) GetName()string{
return p.name
}
The part between the func keyword and the function name, GetName(), is known as the receiver of the method.
Let's say we declare a value of type Person, like this:
var p = Person{
name: "Jason",
age: 29,
}
Now, we can invoke the GetName method of value p, as follows:
p.GetName()
Let's create another method called GetAge(), which returns the age of the attached person. Here is the code to do so:
func (p Person) GetAge()int{
return p.age
}
Now, we'll see what type embedding is.
推薦閱讀
- DevOps:軟件架構(gòu)師行動(dòng)指南
- ClickHouse性能之巔:從架構(gòu)設(shè)計(jì)解讀性能之謎
- Modular Programming with Python
- Hands-On Machine Learning with scikit:learn and Scientific Python Toolkits
- Mastering Selenium WebDriver
- MongoDB for Java Developers
- Learning SAP Analytics Cloud
- Learning AWS Lumberyard Game Development
- 微信公眾平臺(tái)開(kāi)發(fā):從零基礎(chǔ)到ThinkPHP5高性能框架實(shí)踐
- SSM輕量級(jí)框架應(yīng)用實(shí)戰(zhàn)
- Nginx實(shí)戰(zhàn):基于Lua語(yǔ)言的配置、開(kāi)發(fā)與架構(gòu)詳解
- Mastering openFrameworks:Creative Coding Demystified
- 搞定J2EE:Struts+Spring+Hibernate整合詳解與典型案例
- Frank Kane's Taming Big Data with Apache Spark and Python
- Visual Studio Code 權(quán)威指南