Comprehending methods in Go

Methods in Go are functions that have a special type, called a receiver, that sits between the function keyword and the method name associated with the keyword. Go doesn't have classes in the same manner that other programming languages do . Structs are often used in conjunction with methods in order to bundle data and its corresponding methods in a similar fashion to how classes are constructed in other languages. As we instantiate a new method, we can add struct values in order to enrich the function call.

We can instantiate a structure and a method as follows:

package main
import "fmt"
type User struct {
uid int
name string
email string
phone string
}

func (u User) displayEmail() {
fmt.Printf("User %d Email: %s ", u.uid, u.email)
}

After this has been done, we can then use this struct and method to display information about a user as follows:

func main() {
userExample := User{
uid: 1,
name: "bob",
email: "[email protected]",
phone: "123-456-7890",
}

userExample.displayEmail()
}

This will return the result from userExample.displayEmail(), which prints the pertinent part of the structs as part of the method call as follows:

As we have larger structs of data, we have the ability to reference the data that is stored within these structs easily and effectively. If we decided that we wanted to write a method to find the end user's phone number, it would be simple to use our existing data type and write a method similar to the displayEmail method in order to return the end user's phone number.

The methods we have looked at so far only have value receivers. Methods can also have pointer receivers. Pointer receivers are helpful when you would like to update the data in place and have the results available to the caller function.

Consider our previous example with a couple of modifications. We are going to have two methods that will allow us to update our user's email address and phone number. The email address update is going to use a value receiver, whereas the phone update is going to use a pointer receiver.

We create these functions in the following code block in order to be able to update the end user's information easily:

package main 
import "fmt"

type User struct {
uid int
name string
email string
phone string
}

func (u User) updateEmail(newEmail string) {
u.email = newEmail
}

func (u *User) updatePhone(newPhone string) {
u.phone = newPhone
}

We next create our example end user in main, as shown in the following code block:

func main() {
userExample := User{
uid: 1,
name: "bob",
email: "[email protected]",
phone: "123-456-7890",
}

We then update the email and phone number of our end user in the following code block:

userExample.updateEmail("[email protected]") 
(userExample).updatePhone("000-000-0000")
fmt.Println("Updated User Email: ", userExample.email)
fmt.Println("Updated User Phone: ", userExample.phone)
}

In our resulting output, we can see that the user email has not been updated from the perspective of the receiver, but that the user's phone number has:

This is important to remember when attempting to mutate state from within method calls. Methods are very helpful in manipulating data in Go programs.

It's now time to see what inheritance in Go is all about.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.120.206