Go Language
https://golang.org/ref/spec
Table of Contents
- Basic Types and Operators
- Program Initialization and execution
package main
andfunc main() {}
package
andimport
- Package Initialization
- Package Naming
if
/else
for
/range
GOPATH
- Which things are addressable?
- How Go Workspaces(directory structure) are organized
- Does Go support Python-like unpacking(or restructuring)?
- Representability
Basic Types and Operators discussion
bool
string
int int8 int16 int32 int64
uint uint8 uint16 uint32 uint64 uintptr
byte // alias for uint8
rune // alias for int32
// represents a Unicode code point
float32 float64
complex64 complex128
Precedence Operator
5 * / % << >> & &^
4 + - | ^
3 == != < <= > >=
2 &&
1 ||
+ sum integers, floats, complex values, strings
- difference integers, floats, complex values
* product integers, floats, complex values
/ quotient integers, floats, complex values
% remainder integers
& bitwise AND integers
| bitwise OR integers
^ bitwise XOR integers
&^ bit clear (AND NOT) integers
<< left shift integer << unsigned integer
>> right shift integer >> unsigned integer
== equal
!= not equal
< less
<= less or equal
> greater
>= greater or equal
&& conditional AND p && q is "if p then q else false"
|| conditional OR p || q is "if p then true else q"
! NOT !p is "not p"
Program Initialization and execution
- When storage is allocated and there is no explicit initialization is provided, the variable or value is given a default value.
- Within a package, package-level variables are initialized in declaration order but after any of the variables they depend on.
- The declaration order of variables declared in multiple files is determined by the order in which the files are presented to the compiler.
package main
and func main() {}
discussion
A complete program is created by linking a single, unimported package called the main package with all the packages it imports, transitively. The main package must have package name main and declare a function main that takes no arguments and returns no value.
package
and import
reference
- All imports must be explicitly listed at the beginning of each source file, so the compiler does not have to read and process an entire file to determine its dependencies.
- The dependencies of a package form a directed acyclic graph, and because there are no cycles, packages can be compiled separately and perhaps in parallel.
- The
go build
tool reports an error if these dependencies form a cycle. - The object file for a compiled Go package records export information not just for the package itself, but for its dependencies too.
- To avoid conflicts, the import paths of all packages other than those from the standard library should start with the Internet domain name of the organization that owns or hosts the package.
- Conventionally, the package name is the last segment of the import path.
If we need to import two packages whose names are the same, like math/rand
and crypto/rand
, into a third package, the import declaration must specify an alternative name for at least one of them to avoid a conflict:
When needing only the side effect of some pacakges, use blank import:
- http://www.gopl.io/ (10.1)
Package Initialization discussion
- Overall Rules
- Basically, most evaluation order follows the order of declaration in the file.
- But, if some dependencies are related, the dependencies are resolved first.
- For the order between files in a package, the order of evaluation depends on the order of how files are provided to
go build
command.
- Initialization of imported packages (recursive definition)
- Computing and assigning initial values for variables declared in a package block.
- In the order in which they are declared, except that dependencies are resolved first.
- Executing
init
functions inside the packagefunc init()
can be defined several times, no matter where the function is placed.
package main
import "fmt"
func init() {
fmt.Println("init 1")
}
func init() {
fmt.Println("init 2")
}
func main() {
fmt.Println("main")
}
init 1
init 2
init 3
main
Package Naming discussion
- Keep its name short, but not so short as to be cryptic.
- Be descriptive and unambiguous where possible.
- Take the singular form whenever possible.
- Avoid names that already have other connotations.
- http://www.gopl.io/ (10.6)
if
/ else
reference
package main
import "fmt"
func main() {
if 8%4 == 0 {
fmt.Println("8 is divisible by 4")
}
if 7%2 == 0 {
fmt.Println("7 is even")
} else {
fmt.Println("7 is odd")
}
if num := 9; num < 0 {
fmt.Println(num, "is negative")
} else if num < 10 {
fmt.Println(num, "has 1 digit")
} else {
fmt.Println(num, "has multiple digits")
}
}
for
/ range
reference
package main
func main() {
i := 1
// while
for i <= 3 {
}
// classic
for j := 7; j <= 9; j++ {
}
// infinite
for {
}
// can continue
for n := 0; n <= 5; n++ {
if n%2 == 0 {
continue
}
}
nums := []int{2, 3, 4}
for _, num := range nums {} // if idx not needed
for i, num := range nums {}
kvs := map[string]string{"a": "apple", "b": "banana"}
for k, v := range kvs {}
for k := range kvs {} // just keys
for i, c := range "go" {} // 'c' is 'rune' of the string
}
GOPATH
reference
The GOPATH
environment variable specifies the location of your workspace.
- The default value is
$HOME/go
- Put
export PATH=$PATH:$(go env GOPATH)/bin
to rc files for convenience. - Read details with
go help gopath
.
Which things are addressable? discussion
&x // variable
&a[2] // elements of array/slice
&person.name // addressable field of a struct
&Point{2, 3} // composite literal
&*p // pointer indirection
How Go Workspaces(directory structure) are organized tutorial
- Go programmers typically keep all their Go code in a single workspace.
- A workspace contains many version control repositories (managed by Git, for example).
- Each repository contains one or more packages.
- Each package consists of one or more Go source files in a single directory.
- The path to a package's directory determines its import path.
A workspace is a directory hierarchy with three directories at its root:
src
contains Go source files,pkg
contains package objects, andbin
contains executable commands.
bin/
hello # command executable
outyet # command executable
pkg/
linux_amd64/
github.com/golang/example/
stringutil.a # package object
src/
github.com/golang/example/
.git/ # Git repository metadata
hello/
hello.go # command source
outyet/
main.go # command source
main_test.go # test source
golang.org/x/image/
.git/
bmp/
reader.go
writer.go
Does Go support Python-like unpacking(or restructuring)? discussion
- No. Just do it manually:
Representability discussion
x
is in the set of values determined byT
.T
is a floating-point type andx
can be rounded toT
's precision without overflow.
x T x is representable by a value of T because
'a' byte 97 is in the set of byte values
97 rune rune is an alias for int32, and 97 is in the set of 32-bit integers
"foo" string "foo" is in the set of string values
1024 int16 1024 is in the set of 16-bit integers
42.0 byte 42 is in the set of unsigned 8-bit integers
1e10 uint64 10000000000 is in the set of unsigned 64-bit integers
2.718281828459045 float32 2.718281828459045 rounds to 2.7182817 which is in the set of float32 values
-1e-1000 float64 -1e-1000 rounds to IEEE -0.0 which is further simplified to 0.0
0i int 0 is an integer value
(42 + 0i) float32 42.0 (with zero imaginary part) is in the set of float32 values