An Interest In:
Web News this Week
- April 27, 2024
- April 26, 2024
- April 25, 2024
- April 24, 2024
- April 23, 2024
- April 22, 2024
- April 21, 2024
What are methods in golang
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
Lets see a full example to understand how to define a method on a type and how to invoke such a method
package maintype 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 mainimport "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 mainimport "fmt"type User struct { //define a struct FirstName string LastName string}// GetFullName method to get full namefunc (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 mainimport "fmt"type Age intfunc (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.
Original Link: https://dev.to/theamasoud/what-are-methods-in-golang-4b5g
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To