len
function (and cap
, close
, delete
, make
functions)
len | cap | close | delete | make | |
---|---|---|---|---|---|
string |
Yes
|
|
|
|
|
array (and array pointer) |
Yes
|
Yes
|
|
|
|
slice |
Yes
|
Yes
|
|
|
Yes
|
map |
Yes
|
|
|
Yes
|
Yes
|
channel |
Yes
|
Yes
|
Yes
|
|
Yes
|
len
can be called container types in broad sense.
Type | Can New Elements Be Added into Values? | Are Elements of Values Replaceable? | Are Elements of Values Addressable? | Will Element Accesses Modify Value Lengths? | May Values Have Underlying Parts |
---|---|---|---|---|---|
string |
No
|
No
|
No
|
No
|
Yes(1)
|
array |
No
|
Yes(2)
|
Yes(2)
|
No
|
No
|
slice |
No(3)
|
Yes
|
Yes
|
No
|
Yes
|
map |
Yes
|
Yes
|
No
|
No
|
Yes
|
channel |
Yes(4)
|
No
|
No
|
Yes
|
Yes
|
reflect.SetLen
function. Increase the length of a slice by this way is kind of adding new elements into the slice. But the reflect.SetLen
function is slow, so it is rarely used.T{...}
)
Type (T )
|
Is T{} a Zero Value of T ?
|
---|---|
struct |
Yes
|
array |
Yes
|
slice |
No
(zero value is nil )
|
map |
No
(zero value is nil )
|
nil
nil
.
Type (T )
|
Size of T(nil)
|
---|---|
pointer |
1 word
|
slice |
3 words
|
map |
1 word
|
channel |
1 word
|
function |
1 word
|
interface |
2 words
|
Function | Return Type | Are Calls Always Evaluated at Compile Time? |
---|---|---|
unsafe.Sizeof |
uintptr
|
Yes, always.
But please note that the result of such a call is not viewed as a constant if the argument type of the call is a type parameter.
|
unsafe.Alignof | ||
unsafe.Offsetof | ||
len |
int
|
Not always.
From Go specification:
Please note that, even if such a function call is evaluated at compile-time, the evaluation result is not viewed as a constant if the argument type of the call is a type parameter.
|
cap | ||
real |
The result is an untyped value. Its default type is
float64 .
|
Not always.
|
imag | ||
complex |
The result is an untyped value. Its default type is
complex128 .
|
Not always.
|
Allowed to Be Declared but Not Used? | |
---|---|
import |
No
|
type |
Yes
|
variable |
Yes for package-level variables.
No for local variables (for the standard compiler). |
constant |
Yes
|
function |
Yes
|
label |
No
|
()
()
:
()
. Also labels.
Syntax |
Meaning of The Optional Value (ok in the syntax examples)
|
Will Omitting the Optional Result Affect Program Behavior? | |
---|---|---|---|
map element access |
e, ok = aMap[key]
|
whether or not the accessed key is present in the map
|
No
|
channel value receive |
e, ok = <- aChannel
|
whether or not the received value was sent before the channel was closed
|
No
|
type assertion |
v, ok = anInterface.(T)
|
whether or not the dynamic type of the interface value matches the asserted type
|
Yes
(when the optional bool result is omitted, a panic occurs if the assertion fails.) |
make(chan struct{}) <- struct{}{}
// or
make(chan<- struct{}) <- struct{}{}
<-make(chan struct{})
// or
<-make(<-chan struct{})
// or
for range make(<-chan struct{}) {}
chan struct{}(nil) <- struct{}{}
// or
<-chan struct{}(nil)
// or
for range chan struct{}(nil) {}
select{}
The Go 101 project is hosted on Github. Welcome to improve Go 101 articles by submitting corrections for all kinds of mistakes, such as typos, grammar errors, wording inaccuracies, description flaws, code bugs and broken links.
If you would like to learn some Go details and facts every serveral days, please follow Go 101's official Twitter account @zigo_101.
reflect
standard package.sync
standard package.sync/atomic
standard package.nil
in Go