Skip to content

0x345 Go

1. Language

1.1. Variable

Name If an entity dclared within a function, it is local to the function, if outside, it is visible to all files within the same package. The case of the first letter of a name determins its visibility across package: upper-case means exported (e.g.: fmt.Println).

Declaration A var declaration has the general form

var name type = expression

where either the type or = expression may be ommitted. If the expression is omitted, the initial value is zero initialized (unlike C local scope).

The declaration also has a short version

name := expression

where type is determied by the expression. Note that := is declaration and = is assignment in go

Pointer pointer generally work similarly as it does in C The unique features of pointers in go are

  • zero value for pointer is nil
  • pointers are comparable, they are considered equal if they are pointing to the same variable or both nil
  • pointer of a local variable can be returned from function, as pointer are managed by garbage collection.

Life time variable lives on until it becomes unreachable, at which point its storage maybe recycled. Interestingly, a compiler may choose to allocate local variables on the heap or on the stack.

Package Package is initialized in the following order - one package is initialized at a time, in the order of imports in the program, dependencies first - In each pacakge, each variable is initialized in the order of declaration, dependencies first - init function also can be used for declaration.

1.2. Basic Data Type

1.2.1. Integer

Go provides both signed and unsigned integer arithmetic, with 4 distinct sizes 8, 16, 32, 64

  • The type rune is a synonym for int32 and conventionally indicates a unicode code point
  • the type byte is a synonym for uint8
  • uintptr is a type whose width is not specified but is sufficient to hold all bits of a pointer
  • Regardless of their size, int,uint are different types from their explicitly sized siblings, therefore int is not the same type as int32, even if the natural size is 32 bits.

2. Library

3. Reference

  • The Go Programming Language