value parts
The main characteristic of C types is the memory layouts of their values are transparent
Go can also be viewed as C language framework. This is mainly reflected in the fact that Go supports several kinds of types whose value memory layouts are not totally transparent. Each values of the these kinds of types is often composed of one direct part and one or several underlying indirect parts, and the underlying value part is referenced by the direct value part
Two kinds points
- If a struct value
a
has a pointer fieldb
which references a valuec
, then we can say the struct valuea
also (indirectly) references valuec
. - If a value
x
references (either directly or indirectly) a valuey
, and the valuey
references (either directly or indirectly) a valuez
, then we can also say the valuex
(indirectly) references valuez
Internal Definitions of the types
1 | // map types |
underlying value parts are not copied in value assignments
In Go, each value assignment (including parameter passing, etc) is a shallow value copy if the involved destination and source values have the same type (if their types are different, we can think that the source value will be implicitly converted to the destination type before doing that assignment). In other words, only the direct part of the soruce value is copied to the destination value in an value assignment. If the source value has underlying value part(s), then the direct parts of the destination and source values will reference the same underlying value part(s), in other words, the destination and source values will share the same underlying value part(s).
Here I just list some absolutely misuses of reference
- only slice, map, channel and function types are reference types in Go. (If we do need the reference type terminology in Go, then we shouldn’t exclude any custom pointer and pointer wrapper types from reference types).
- references are opposites of values. (If we do need the reference value terminology in Go, then please view reference values as special values, instead of opposites of values.)
- some parameters are passed by reference. (Sorry, all parameters are passed by copy in Go.)
line break rules in go
One rule we often obey in practice is, we should not put the a starting curly brace ({
) of the explicit code block of a control flow block on a new line.
1 | for i := 5; i > 0; i-- { |
However, there are some exceptions for the rule mentioned above. For example, the following bare for
loop block compiles okay.
1 | for |
活用;
Some Panic/Recover Use Cases
avoid panics crashing programs
1 | package main |
automatically restart a crash goroutine
1 | package main |
use panic/recover to reduce error checkings
1 | package main |
close channel
One general principle of using Go channels is don’t close a channel from the receiver side and don’t close a channel if the channel has multiple concurrent senders. In other words, you should only close a channel in a sender goroutine if the sender is the only sender of the channel.