Back to course

Methods: Adding Behavior to Structs

Go (Golang) for Cloud-Native Microservices

Methods in Go

You can attach functions to structs. These are called methods.

go func (u User) Greet() string { return "Hello, " + u.Name }

Pointer Receivers:

If you want to modify the struct inside the method, use a pointer receiver: go func (u *User) UpdateEmail(newEmail string) { u.Email = newEmail }