Simple Summaries

Index

Besides the current article, there also some other summary articles for some special topics, including

Types whose values may have indirect underlying parts

Types whose values may have indirect underlying parts:

This article lists the internal definitions of above types by the standard Go compiler/runtime.

Types which values can be used as arguments of built-in function len (and functions cap, close, delete, make)

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

Values of above types can also be ranged over in for-range loops.

Types which values can be used as arguments of built-in function len can be called container types.

Comparison of built-in container types

Type Can New Elements Be Added Into Values? Are Elements Of Values Replaceable? Are Elements Of Values Addressable? Will Element Accessments 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

(1) for the standard Go compiler/runtime.
(2) for addressable array values only.
(3) slice lengths can be modified by using reflect.SetLen function. Increase the length of a slice by this way is kind of adding new elements into the slice. The length of a slice value can also be modified by assigning another slice value to it.
(4) for buffered channels which are still not full.

Types which values can be represented with composite literals (T{...})

Values of following types can be represented with composite literals:

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)

Value sizes of all kinds of types

Please read value copy cost for details.

Types which zero values can be represented with nil

The zero values of the following types can be represented with 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

(One word means 4 bytes on 32bits OSes and 8 bytes on 64bits OSes. and the indirect underlying parts of a value don't contribute to the size of the value.)

The size of a zero value of a type is the same as any other values of the same type.

Types we can implement methods for

Please read this question for details.

Types which can be anonymously embedded in other types

Please read which types can be embedded for details.

Functions whose calls will/may be evaluated at compile time

If a function call is evaluated at compile time, its return results must be constants.

Function Return Type Are Calls Always Evaluated At Compile Time?
unsafe.Sizeof uintptr Yes, always.
unsafe.Alignof
unsafe.Offsetof
len int Not always.

From Go spec:
  • the expression len(s) is constant if s is a string constant.
  • the expressions len(s) and cap(s) are constants if the type of s is an array or pointer to an array and the expression s does not contain channel receives or (non-constant) function calls (in other words, s can be array variables and arry pointer variables).
cap
real float64
(default type)
Not always.

From Go spec: the expressions real(s) and imag(s) are constants if s is a complex constant.
imag
complex complex128
(default type)
Not always.

From Go spec: the expression complex(sr, si) is constant if both sr and si are numeric constants.

Values that can't be taken addresses

Following values can't be taken addresses:
Please note, there is a syntax sugar, &T{}, in Go. It is a short form of tmp := T{}; (&tmp), so &T{} is legal doesn't mean the composite literal T{} is addressable.

BTW, following values can be taken addresses (except the just mentioned composite literals):

Types which don't support comparisons

Following types don't support comparisons:

Types which don't support comparisons can't be used as the key types of map types.

Please note,

Which code elements are allowed to be declared but not used

Allowed To Be Declared But Not Used
import No
type Yes
variable Yes for package-level variables.
No for local variables.
constant Yes
function Yes
label No

Named source code elements which declarations can be grouped with ()

Following named source code element declarations can be grouped with ():

Functions and labels can't be grouped with ().

Named source code elements which can be declared both inside functions and outside any functions

Following named source code elements can be declared both inside functions and outside any functions:

Imports must be declared before declarations of other elements (and after the package clause).

Functions can only be declared outside any functions. Anonymous functions can be defined inside other function bodies, but they are not declared.

Labels must declared inside functions.

Expressions which can return an optional additional bool return value

Following expressions can return an optional additional bool return value:

Meaning Of The Optional Return Will Omitting The Optional Return Affect Program Behavior?
map element access whether or not the accessed key is present in the map No
channel value receive whether or not the received value was sent before the channel was closed No
type assertion whether or not the dynamic type of the interface value matches the asserted type Yes
(when the optional bool return is omitted, a panic is created if the asserted type is not an ok type)

Ways to block current goroutine ‎forever by using the channel mechanism

Following ways can be used to block current goroutine ‎forever:
  1. receive from a channel which no values will be sent to
    <-make(chan struct{})
    // or
    <-make(<-chan struct{})
    
  2. send value to a channel which no ones will receive values from
    make(chan struct{}) <- struct{}{}
    // or
    make(chan<- struct{}) <- struct{}{}
    
  3. receive value from a nil channel
    <-chan struct{}(nil)
    // or
    for range chan struct{}(nil) {}
    
  4. send value to a nil channel
    chan struct{}(nil) <- struct{}{}
    
  5. use a bare select block
    select{}
    

Ways to concatenate strings

Please read strings in Go for details.


The Go 101 project is hosted on both github and gitlab. 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.

Support Go 101 by playing Tapir's games. Cryptocurrency donations are also welcome:
Bitcoin: 1xucQbv5jujFPPwhyg395ri5yV71hx9g9
Ethereum: 0x5dc4aa2c2bbfaadae373dadcfca11b3358912212