Skip to content

Instantly share code, notes, and snippets.

@vbauerster
Created November 21, 2016 11:23
Show Gist options
  • Select an option

  • Save vbauerster/cc6db06a895f2dbcb430bbe0c635210d to your computer and use it in GitHub Desktop.

Select an option

Save vbauerster/cc6db06a895f2dbcb430bbe0c635210d to your computer and use it in GitHub Desktop.
package main
import "fmt"
type person struct {
Name string
Age int
}
func main() {
students := make(map[int]person)
students[0] = person{Name: "John"}
fmt.Printf("students[0] = %+v\n", students[0]) // {Name:John Age:0}
// students[0].Age = 22 // error: cannot assign to struct field
john := students[0]
john.Age = 22
fmt.Printf("students[0] = %+v\n", students[0]) // {Name:John Age:0}
students[0] = john
fmt.Printf("students[0] = %+v\n", students[0]) // {Name:John Age:22}
pstudents := make(map[int]*person)
pstudents[0] = &person{Name: "Criss"}
fmt.Printf("students[0] = %+v\n", pstudents[0]) // &{Name:Criss Age:0}
pstudents[0].Age = 33
fmt.Printf("students[0] = %+v\n", pstudents[0]) // &{Name:Criss Age:33}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment