Last active
March 25, 2020 20:37
-
-
Save estysdesu/6e0af7829c81feaad03e47256f7a9a8f to your computer and use it in GitHub Desktop.
[Go: Ellipsis, JSON/Unmarshalling] #go #golang #ellipsis #json #pointers
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
// vim: syntax=go | |
// ***** 3 PERIODS (ELLIPSIS) UNPACK A VARIABLE ***** | |
primes := []int{2, 3, 5, 7} | |
fmt.Println(Sum(primes...)) // 17 | |
// ***** JSON STRUCTS ***** | |
// A field with a json: struct tag is stored with its tag name instead of its variable name. | |
type user struct { | |
ID int `json: "ref"` | |
name string | |
name2 string `json: "name2"` | |
} | |
// When `json.Marshal` is run ID is put as a `ref` key in json. | |
// However, `name` will not be put in the json because it is lowercase and therefore not exported. | |
// But, because `name2` has a declared json field it does get exported. | |
// ***** POINTERS & REFERNCING/DEREFERENCING ***** | |
// `&` is a pointer --> `b = &a` == memory address of a | |
// `*` gets the pointers underlying value, aka "dereferencing" --> `*b` gives the values in the memory of a | |
// `func (c *Config) Read(p string) {}` --> c is a pointer receiver, meaning &c changes the actual values of the config struct |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment