Methods

Download go eBook

Example

Struct methods are very similar to functions:

type User struct {
    name string
}

func (u User) Name() string {
    return u.name
}

func (u *User) SetName(newName string) {
    u.name = newName
}

The only difference is the addition of the method receiver. It may be declared either as an instance of the type or a pointer to an instance of the type. Since SetName() mutates the instance, the receiver must be a pointer in order to effect a permanent change in the instance.

For example:

package main

import "fmt"

type User struct {
    name string
}

func (u User) Name() string {
    return u.name
}

func (u *User) SetName(newName string) {
    u.name = newName
}

func main() {
    var me User

    me.SetName("Slim Shady")
    fmt.Println("My name is", me.Name())
}

Go Playground

Stats

Contributors: 5
2017-03-07
Licensed under: CC-BY-SA

Not affiliated with Stack Overflow
Rip Tutorial: info@zzzprojects.com

Download eBook