Skip to content

Instantly share code, notes, and snippets.

@iMega
Created July 26, 2018 11:13
Show Gist options
  • Save iMega/e44b8b28024019c99385a29d9b01841a to your computer and use it in GitHub Desktop.
Save iMega/e44b8b28024019c99385a29d9b01841a to your computer and use it in GitHub Desktop.
Утренний затуп
package main
import (
"fmt"
)
type Person struct{
Name string
}
// переменная со своей областью памяти 0x17d010 хранящей Person
var person = Person{
Name: "Pedro",
}
// переменная со своей областью памяти 0x183cec, которая будет хранить указатель на Person (0x17d010)
var personRef *Person
func main() {
fmt.Printf("person: type = %T; address = %p; value = %+v\n\n", person, &person, person)
fmt.Printf("personRef: type = %T; address = %p; value = %+v\n\n", personRef, &personRef, personRef)
m1 := map[string]*Person{
"qwe": personRef, // в qwe помещается текущее значение ячейки памяти 0x183cec (nil, так как указатель ещё не установлен и не существует)
}
fmt.Printf("m1[\"qwe\"]: type = %T; address = %p; value = %+v\n\n", m1["qwe"], m1["qwe"], m1["qwe"])
m2 := map[string]**Person{
"qwe": &personRef, // в qwe помещается указатель на значение ячейки памяти 0x183cec (nil, так как указатель ещё не установлен и не существует)
}
fmt.Printf("m2[\"qwe\"]: type = %T; address = %p; value = %+v\n\n", m2["qwe"], m2["qwe"], *m2["qwe"])
personRef = &person // в m1["qwe"] по прежнему nil, а вот в m2["qwe"] появилось значение
fmt.Printf("personRef: type = %T; address = %p; value = %+v\n\n", personRef, &personRef, personRef)
fmt.Printf("m1[\"qwe\"]: type = %T; address = %p; value = %+v\n\n", m1["qwe"], m1["qwe"], m1["qwe"])
fmt.Printf("m2[\"qwe\"]: type = %T; address = %p; value = %+v", m2["qwe"], m2["qwe"], *m2["qwe"])
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment