You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Dereferencing (getting/setting the memory value) from a pointer stored in variable p: *p
// Printing the memory address from variable `v`varvint=42fmt.Println(&v)
// Declaring a pointer to an `int` and assigning address of `v`varp*int=&v// Printing the value that `p` points tofmt.Println(*p) // "42"// Changing the value of `v` using `p`*p=27fmt.Println(v) // "27"
Pointer arithmetic is not allowed. So g := &e[1] - 1 will not work.
Before initialising a pointer variable, it is set to nil, which is the "zero value" for pointers.
Once we use new() to create an empty struct inside it, it does have the "zero value" for the struct.
Go displays pointers with a & symbol when printing a struct pointer as well as the value of the struct itself.
Accessing the value of a struct field from its pointer would require using parenthesis (*ms).foo, but Go has some syntax sugar that makes it possible to simply write ms.foo.
In functions where values are passed by reference, we usually need to explicitly check for nil values.
Reference and Value types
slice and map are reference types, whereas array and struct are value types. This means that you must be careful when operating with slices and maps as to who has access to the information.structs and arrays only have this problem when you're explicitly using pointers.
This means that whenever you're passing a slice or map inside your application, the data is always going to be the same, it will not be copied across, but rather passed by reference.