Go programs are organized into packages. A package is a collection of source files in the same directory that are compiled together. Functions, types, variables, and constants defined in one source file are visible to all other source files within the same package.
设置GOBIN和取消GOBIN
You can use the go env command to portably set the default value for an environment variable for future go commands:
$ go env -w GOBIN=/somewhere/else/bin
$
To unset a variable previously set by go env -w, use go env -u:
$ go env -u GOBIN
factored import statement.
Notice that the type comes after the variable name.
连续变量,公用类型,省略前面的类型。
When two or more consecutive named function parameters share a type, you can omit the type from all but the last.
Go's return values may be named. If so, they are treated as variables defined at the top of the function.
A return statement without arguments returns the named return values. This is known as a "naked" return.
Naked return statements should be used only in short functions, as with the example shown here. They can harm readability in longer functions.
The var statement declares a list of variables; as in function argument lists, the type is last.
A var statement can be at package or function level.
短的变量声明只能在function内使用
Inside a function, the := short assignment statement can be used in place of a var declaration with implicit type.
Outside a function, every statement begins with a keyword (var, func, and so on) and so the := construct is not available.
Go's basic types are
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
Zero values
Variables declared without an explicit initial value are given their zero value.
The zero value is:
0for numeric types,falsefor the boolean type, and""(the empty string) for strings.
Type conversions
The expression T(v) converts the value v to the type T.
类型推断
But when the right hand side contains an untyped numeric constant, the new variable may be an int, float64, or complex128 depending on the precision of the constant:
i := 42 // int
f := 3.142 // float64
g := 0.867 + 0.5i // complex128
常量
Constants
Constants are declared like variables, but with the const keyword.
Constants can be character, string, boolean, or numeric values.
Constants cannot be declared using the := syntax.
For 循环
The init statement will often be a short variable declaration, and the variables declared there are visible only in the scope of the for statement.
没有圆括号,有大括号
The init and post statements are optional.
For is Go's "while"
At that point you can drop the semicolons: C's while is spelled for in Go.
Forever
If you omit the loop condition it loops forever, so an infinite loop is compactly expressed.
If
Go's if statements are like its for loops; the expression need not be surrounded by parentheses ( ) but the braces { } are required.
If with a short statement
Like for, the if statement can start with a short statement to execute before the condition.
Variables declared by the statement are only in scope until the end of the if.
If and else
Variables declared inside an if short statement are also available inside any of the else blocks.
Switch
A switch statement is a shorter way to write a sequence of if - else statements. It runs the first case whose value is equal to the condition expression.
不需要break,是自动加上的。
A defer statement defers the execution of a function until the surrounding function returns.
Go has pointers. A pointer holds the memory address of a value.
The type *T is a pointer to a T value. Its zero value is nil.
The & operator generates a pointer to its operand.
The * operator denotes the pointer's underlying value.
A struct is a collection of fields.
Pointers to structs
p.X
Struct Literals
An array's length is part of its type
An array has a fixed size. A slice, on the other hand, is a dynamically-sized, flexible view into the elements of an array. In practice, slices are much more common than arrays.
The type []T is a slice with elements of type T.
A slice does not store any data, it just describes a section of an underlying array.
Slice defaults
When slicing, you may omit the high or low bounds to use their defaults instead.
The default is zero for the low bound and the length of the slice for the high bound.
If you only want the index, you can omit the second variable.
Go interprets the statement v.Scale(5) as (&v).Scale(5) since the Scale method has a pointer receiver.