This is a creation in Article, where the information may have evolved or changed.
- Go language
- Characteristics
- Chapter1
- Chatper2 Basic Syntax
- Special reserved Keywords
- Tips
- Variable request
- Built-in types
- Process Control
- If
- For
- Break Goto can support tags
- Switch
- Fallthrough
- Func function definition
- Defer delay
- Panic recover Try Catch
- Import operation
- struct Custom type
Go language
Characteristics:
Minimalist syntax, native multithreading support;
Words of Praise:
Explanatory flexibility, dynamic type language efficiency, static type of security;
Chapter1:
Gopath is the working directory of the Go language;
* Basic FILE--$GOPATH/src, pkg, bin
* Go BUILD: Compile, if the main package generates the same executable file with the same directory name
* Go Install build Package
* Go FMT can automatically format code style;
Tips
- Array_linux.go Array_darwin.go build changes to the same compilation as the current system environment;
chatper2--Basic Syntax
Go default UTF-8 encoding;
Special reserved Keywords:
Chan, defer, Fallthrough, map, range, select,
Tips
- Fmt. Printf does not wrap automatically;
- Python's print will be automatically wrapped;
- _, B = 34, 35; # Variables assigned to "_" are discarded
- Unused variables, compile errors (good coercion);
- Int32 and int 64 cannot be added or reduced (no cast);
- Support plural: var c complex64 = 5 + 5i;
- The string anti-quote "is raw_string, not escaped;
- Iota is reset to 0 each time the value is assigned;
- array fixed length;
- Slice has a cap concept; When the cap is 0, slice.append () will open up new memory;
Variable request
vartypevartype'1''2'var'1''2''1''2'
var variable type; variable declaration at the end;
The default value can not be declared when the type is provided;
does not even provide keyword Var
VAR1, Var2: = ' 1 ', ' 2 '
The ": =" assignment is equivalent to defining the variable, but only for local variables;
Global variables must be explicitly declared with Var;
var variable;
Const constants;
Built-in types:
- Bool:true, False
- int, uint; and various fixed length int32, Int64; (non-additive between various int)
- String: Double quotation mark "", "inverted quotation mark" enclosed; (no single quotation mark) immutable;
Change string, must become byte array, modified, then New_str = string (Btye_array)
- Error: Wrong type
- Array:var arr [N]type;
- Slice: Dynamic array, var arr []type;
- Map: Python dict; Rating: = map[string]float32 {"C": 4.5, "GO": 5}
The memory allocation of the go language-a separate area for each data type (so no value can be assigned between different int)
Iota enumeration values
const ( iota iota iota w) # x:0, y:1, z:2, w:3constiota # 此时iota重置为0, t:0
Convention:
Capital letters start with public; for example, Sqrt
Lowercase letters start with Private; calculate
Process Control:
If
Conditional left and right no brackets ()
The condition is a declared variable (local variable, only valid within the IF statement block)
For
for i=0; i < 1000; i++ {}for ; sum < 1000; { sum += 10}可简写为:for sum < 1000; { sum += 10}# 其实就是 while(囧)
Break, Goto can support tags;
Switch
The break action is added by default for each case (you don't have to write so many break at last)
Fallthrough
Force switch case to continue execution after matching, do not automatically break;
Func function definition
func funcName(input1 type1, input2 type2) (output1 type1, output2 type2) { return value1, value2}
Feelings: Embarrassed! Why put the return value behind the parameter;
The biggest benefit of naming the return value is that the Func is visible and can return directly;
Defer (delayed)
Executes in reverse order at the end of the function; (Advanced post-exit)
Often used for resource closure;
Panic + recover = Try catch;
Each package recommends an INIT function (like the init. py function in the Python package)
The main function is only available in the main package;
Import Operation:
import ( "fmt" "net/http" _ "github.com/ziutek/mymysql/godrv")# f 此时为 fmt 的别名; . 为当前目录的意思,http包里函数可以直接使用;# 同python 中 from net.http import *# _ 不引入变量,仅执行 init 函数;
struct custom type;
typestruct { string int int}typestruct { Human string}
The anonymous definition is equivalent to the inheritance of other languages; Student default contains all fields of human;
# method 只需要在函数前面增加一个 receiver 即可;funcfloat64 { return c.radius * c.radius * math.Pi}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.