When to use?

Here are some of the common situations where you'll need to use a pointer instead of a value:

Changing an object's state: Say you have a structure like this


type Person struct {
    name     string
    age      int
    gender   string

}
                        

Now, you want a method that allows you to change a person's name: For this, a pointer is needed, as it gives access to the original instance, not a copy of it:


func (p *Person) changeName(name string){
    p.name = name
}
                        
when_use.go