Contents
Outline
In this blog post, I will introduce what Method
is and how to use Method in Golang. You can see the full source code of this blog post on the link below.
Method
In Golang, the method is a kind of function. The function is normally independent from the type, but the method is associated with the type. In Golang, Any type can have methods.
You can define the method like the below.
// func RECEIVER METHOD_NAME
func (r Person) greeting() string {
return r.width * r.height
}
The receiver can be all local types in the package(defined types in the package), and the method is associated with the receiver.
To check this, create the main.go
file and modify it like the below.
package main
import "fmt"
type Person struct {
Name string
Age int
}
func (r Person) greeting() string {
return "Hello, " + r.Name
}
func main() {
p := Person{Name: "Rob", Age: 4}
fmt.Println(p.greeting())
}
When the code is executed, you can see the result like the below.
# go run main.go
Hello, Rob
Object Oriented Programming
Golang is an Object Oriented Programming language(OOP). In here, the object means that it has data(State) and features(Function).
If Golang doesn’t have the method, the data(type) and features(functions) exist separately. And then, Golang can’t create the object, and we can’t do Object Oriented Programming.
Via the method in Golang, we can make the dependent functions and do the Object Oriented Programming. Also, we can define the relationship between the data like the below.
// Student <- Enroll -> Course
func (s * Student) Enroll(c *Course) {
...
}
// Student <- SendReport -> Professor, Report
func (s * Student) SendReport(p *Professor, r *Report) {
...
}
Of course, we can make the function that has the same feature as the method. Functionally, functions and methods are quaivalent. But, the functions simply operate on the given data, and the only difference is that methods are depenet on the data.
Pointer type method and value type method
When you do programming in Golang, you realize that there are pointer type methods and value type methods. In Golang, the pointer type methods and value type methods are mainly used according to the nature of the type.
When the method is used and the new instance need to be created, the value type method is used. When the new instance is not needed, the pointer type method is used.
type Person struct {
Name string
Age int
}
func (p *Person) AddAge() {
p.Age += 1
}
For example, there is the Person
type. When the person data is created with the Person type and the data age is increased, the person should be same, so in this case, the pointer type method is used.
type Temperature float64
func (t Temperature) Up(temp float64) Temperature {
return t + Temperature(temp)
}
And, for the Temperature
type, the temperature is increased, the old temperature and increased temparature are conceptually different, It is appropriate to use value type methods in here.
To check this, modify the main.go
file like the below.
package main
import "fmt"
type Person struct {
Name string
Age int
}
func (p *Person) AddAge() {
p.Age += 1
}
type Temperature float64
func (t Temperature) Up(temp float64) Temperature {
return t + Temperature(temp)
}
func main() {
p1 := Person{Name: "Rob", Age: 4}
fmt.Println("P1: ", p1)
p1.AddAge()
fmt.Println("P1: ", p1)
t := Temperature(30.0)
fmt.Println("T: ", t)
t = t.Up(4.0)
fmt.Println("T: ", t)
}
When you execute the code, you can see the following result.
# go run main.go
P1: {Rob 4}
P1: {Rob 5}
T: 30
T: 34
Constructor and destructor
In Golang, there are no constructor and destructor. So, if you need the constructor, you should provide a function that works like the constructor.
To check this, create the group/main.go
file and modify it like the below.
package group
type Member struct {
Name string
Age int
}
func NewMember(name string, age int) *Member {
return &Member{Name: name, Age: age}
}
In here, the NewMember
function is the role of constructor.
And then, execute the command below to make the module.
go mod init github.com/dev-yakuza/study-golang/method/group
go mod tidy
Next, let’s create the main
package to use the constructor. Create the main/main.go
file on the same location of the group
folder and modify it like the below.
package main
import (
"fmt"
"github.com/dev-yakuza/study-golang/method/group"
)
func main() {
member1 := group.Member{Name: "John", Age: 20}
member2 := group.NewMember("Paul", 30)
fmt.Println(member1)
fmt.Println(member2)
}
And then, execute the command below to create the module and bind the local module.
go mod init github.com/dev-yakuza/study-golang/method/main
go mod edit -replace github.com/dev-yakuza/study-golang/method/group=../group
go mod tidy
And then, when you execute the command below to execute the program.
go run main.go
You can see the following result.
{John 20}
&{Paul 30}
If you want to know more details about how to use Module in Golang, see the following link.
Embedded field method
In Golang, the struct can have Embedded field
. If you want to know more details about Embedded field
, see the following link.
The struct can have Embedded field
like the below, and Embedded field struct can have the method like the below.
type Member struct {
Name string
Age int
}
func (p *Member) AddAge() {
p.Age += 1
}
type Group struct {
Member
Grade int
}
In this case, you can use the Embedded field method like the below.
g := Group{Member: Member{Name: "John", Age: 20}, Grade: 1}
g.AddAge()
fmt.Println(g)
To check this, modify the main.go
file like the below.
package main
import "fmt"
type Member struct {
Name string
Age int
}
func (p *Member) AddAge() {
p.Age += 1
}
type Group struct {
Member
Grade int
}
func main() {
g := Group{Member: Member{Name: "John", Age: 20}, Grade: 1}
fmt.Println(g)
g.AddAge()
fmt.Println(g)
}
And when you execute the code, you can see the result like the below.
# go run main.go
{{John 20} 1}
{{John 21} 1}
Completed
Done! we’ve seen how to define Method and how to use it. Also, we’ve seen the characteristics of method and how to create the constructor.
Was my blog helpful? Please leave a comment at the bottom. it will be a great help to me!
App promotion
Deku
.Deku
created the applications with Flutter.If you have interested, please try to download them for free.