현재 3권의 신간들인 Go Optimizations 101, Go Details & Tips 101Go Generics 101이 출간되어 있습니다. Leanpub 서점에서 번들을 모두 구입하시는 방법이 비용 대비 효율이 가장 좋습니다.

Go에 대한 많은 정보들과 Go 101 책들의 최신 소식을 얻으시려면 Go 101 트위터 계정인 @Go100and1을 팔로잉 해주세요.

Some Simple Summaries

Index

Types whose values may have indirect underlying parts

Types whose values may have indirect underlying parts:
  • string types
  • function types
  • slice types
  • map types
  • channel types
  • interface types

The answer is based on the implementation of the standard Go compiler/runtime. In fact, whether or not function values may have indirect underlying parts is hardly to prove, and string values and interface values should be viewed as values without indirect underlying parts in logic. Please read value parts for details.

Types which values can be used as arguments of built-in 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

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 in broad sense.

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 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

(1) For the standard Go compiler/runtime.
(2) For addressable array values only.
(3) Generally, a slice value are modified by assigned another slice value to it by overwriting it. Here, such cases are not viewed as "add new elements". In fact, slice lengths can also be modified separately by calling the 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.
(4) For buffered channels which are still not full.

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

Values of the following four kinds of 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

The above listed sizes are for the standard Go compiler. One word means 4 bytes on 32-bit architectures and 8 bytes on 64-bit architectures. 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 methods in Go for details.

Types which can be embedded in struct 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 specification:
  • 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.


cap



real

The result is an untyped value. Its default type is float64. Not always.

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

imag

complex The result is an untyped value. Its default type is complex128. Not always.

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

Addressable and unaddressable values

Please read this FAQ item to get which values are addressable or unaddressable.

Types which don't support comparisons

Please read this FAQ item to get which values are addressable or unaddressable.

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 (for the standard compiler).
constant Yes
function Yes
label No

Named source code elements which can be declared together within ()

Following source code elements (of the same kind) can be declared together within ():
  • import
  • type
  • variable
  • constant

Functions can't be declared together within (). Also labels.

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:
  • type
  • variable
  • constant

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 such definitions are not function declarations.

Labels must be declared inside functions.

Expressions which evaluation results may contain optional additional values

The evaluation results of the following expressions may contain optional additional values:

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.)

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

Without importing any package, we can use the following ways to make the current goroutine ‎enter (and stay in) blocking state forever:
  1. send a value to a channel which no ones will receive values from
    make(chan struct{}) <- struct{}{}
    // or
    make(chan<- struct{}) <- struct{}{}
    
  2. receive a value from a never-closed channel which no values have been and will be sent to
    <-make(chan struct{})
    // or
    <-make(<-chan struct{})
    // or
    for range make(<-chan struct{}) {}
    
  3. receive a value from (or send a value to) a nil channel
    chan struct{}(nil) <- struct{}{}
    // or
    <-chan struct{}(nil)
    // or
    for range chan struct{}(nil) {}
    
  4. use a bare select block
    select{}
    

Ways to concatenate strings

Please read strings in Go for details.

Optimizations made by the standard Go compiler

Please read the Go 101 wiki article for this summary.

Run-time panic and crash cases

Please read the Go 101 wiki article for this summary.


Index↡

The Go 101 프로젝트는 Github 에서 호스팅됩니다. 오타, 문법 오류, 부정확한 표현, 설명 결함, 코드 버그, 끊어진 링크와 같은 모든 종류의 실수에 대한 수정 사항을 제출하여 Go 101을 개선을 돕는 것은 언제나 환영합니다.

주기적으로 Go에 대한 깊이 있는 정보를 얻고 싶다면 Go 101의 공식 트위터 계정인 @go100and1을 팔로우하거나 Go 101 슬랙 채널에j가입해주세요.

이 책의 디지털 버전은 아래와 같은 곳을 통해서 구매할 수 있습니다.
Go 101의 저자인 Tapir는 2016년 7월부터 Go 101 시리즈 책들을 집필하고 go101.org 웹사이트를 유지 관리하고 있습니다. 새로운 콘텐츠는 책과 웹사이트에 수시로 추가될 예정입니다. Tapir는 인디 게임 개발자이기도 합니다. Tapir의 게임을 플레이하여 Go 101을 지원할 수도 있습니다. (안드로이드와 아이폰/아이패드용):
  • Color Infection (★★★★★), 140개 이상의 단계로 이루어진 물리 기반의 캐주얼 퍼즐 게임
  • Rectangle Pushers (★★★★★), 2가지 모드와 104개 이상의 단계로 이루어진 캐주얼 퍼즐 게임
  • Let's Play With Particles, 세가지 미니 게임이 있는 캐주얼 액션 게임
페이팔을 통한 개인 기부도 환영합니다.

색인: