Created
November 21, 2016 11:23
-
-
Save vbauerster/cc6db06a895f2dbcb430bbe0c635210d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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