- 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.
推薦閱讀
- 觸·心:DT時代的大數據精準營銷
- PyTorch自動駕駛視覺感知算法實戰
- Python自動化運維快速入門
- 深入淺出Spring Boot 2.x
- 64位匯編語言的編程藝術
- JavaScript入門經典
- Learning Concurrent Programming in Scala
- Test-Driven Machine Learning
- Python編程入門(第3版)
- JavaScript前端開發基礎教程
- Oracle SOA Suite 12c Administrator's Guide
- HTML5程序設計基礎教程
- Mobile Test Automation with Appium
- 中小企業網站建設與管理(靜態篇)
- Unity Certified Programmer:Exam Guide