Hi, I'm Abdulrahman I'm glad to share this blog, in this blog we will talk about methods in golang.
so first we must know Go does not have classes because it doesn't support Object-oriented programming.
What is Object-oriented
Object-oriented its way to allow programmers to declare a function inside the class definition. Go doesn't allow us to do that, we have to declare a method on a struct via a special syntax
because Golang doesn't support Object-oriented programming but Golang has Methods.
What is Methods in golang
Method is a function with a special receiver
argument and this method can access the properties of the receiver. Receiver here can be type struct or non type struct like(int-string-etc...)
let's see the syntax with example
Syntax
func (r ReceiverType) funcName(parameters) (result){/*code*/}
this above example is ho we define a method for any Receiver here is => YourStruct
Letโs see a full example to understand how to define a method on a type and how to invoke such a method
package main
type User struct { //define a struct
FirstName string
LastName string
}
in above example we defined a struct has FirstName-LastName
and if we want to get the FullName we can get them like this:
package main
import "fmt"
type User struct {
FirstName string
LastName string
}
func main() {
user := User{
"Abdulrahman",
"Masoud",
}
fmt.Print(user.FirstName+" "+user.LastName)
// Result => Abdulrahman Masoud
}
but in GoLang you are allowed to define a method whose receiver is your struct like we say before.
This receiver is accessible inside the method as shown in the below example:
package main
import "fmt"
type User struct { //define a struct
FirstName string
LastName string
}
// GetFullName method to get full name
func (u User) GetFullName() string {
return u.FirstName + " " + u.LastName
}
func main() {
user := User{
"Abdulrahman",
"Masoud",
}
fmt.Print(user.GetFullName())
// Result => Abdulrahman Masoud
}
In above example we defined a struct that has the inputs that I need and second, we defined a GetFullName
method and add a receiver for this method (u User) => this is the receiver of method
Let's define non type struct
Syntax
package main
import "fmt"
type Age int
func (a Age) GetAgeAfterTenYears() int {
return int(a + 10)
}
func main() {
age := Age(22)
fmt.Print(age.GetAgeAfterTenYears())
// Result => 32
}
In above example we defined an Age type it's not a struct type its non type struct like we say before Receiver here can be type struct or non type struct
End of blog
Summary
- Go does not have classes because it doesn't support Object-oriented
- Golang doesn't support Object-oriented programming but Golang has Methods
- Method is a function with a special receiver
- Receiver is type
- Receiver can be type struct or non type struct
- method can access the properties of the receiver
I hope you enjoy this kickoff to a new blog in Golang,
Looking forward to sharing these blogs with you.
Top comments (4)
Thanks for the useful post Abdulrahman!
Amazing explanation, keep it up my friend
Great work ๐๐๐
Great post, Abdulrahman, keep posting valuable information, good luck