June 30, 2018
Basic commands
- Go build is used to compile source files, code packages, dependency packages
The path to the executable file is$GOPATH/bin
- Go run to compile and run the Go source file
- Go get dynamic Get Remote code package
System keyword or reserved word
1, reserved keywords
|
|
|
|
|
| Break |
Default |
Func |
Interface |
Select |
| Case |
Defer |
Go |
Map |
struct |
| Chan |
Else |
Goto |
Package |
Switch |
| Const |
Fallthrough |
If |
Range |
Type |
| Continue |
For |
Import |
Return |
Var |
2, the predetermined identifier, including the underlying data type and the system inline function.
|
|
|
|
|
|
| Append |
bool |
Byte |
Cap |
Close |
Complex |
| Complex64 |
complex128 |
UInt16 |
Copy |
False |
Float32 |
| Float64 |
Imag |
Int |
int8 |
Int16 |
UInt32 |
| Int32 |
Int64 |
Iota |
Len |
Make |
New |
| Nil |
Panic |
UInt64 |
Print |
println |
Real |
| Recover |
String |
True |
UInt |
Uint8 |
Uintprt |
3, inline data type
Boolean type:
The system defines two constants for this type: true and false.
The initial default value is: false.
Format the output when the format string is:%t.
Instance code:
The output is:
- Flag1 = True
- !flag1 = False
- GlobalFlag = True
- (Flag1 && Flag2) = False
- (Flag1 | | flag2) = True
BYTE type:
- For Uint8, that is, there are only 8 bit.
Number type:
- Integer type:
Signed integers: int8, Int16, Int32, Int64, and int (this type may vary depending on the specific platform)
unsigned integers: uint8, uint16, UInt32, UInt64, and uint (this type may vary depending on the specific platform)
The initial default value is: 0.
Format the output when the format string is:%d, output 16:%x or%x;8 binary:%o.
Instance code:
The output is:
I8=9, i=27, ui32=30, ui64=40, defint=0
i8=11, i=1b, ui32=1e, ui64=101000
The value=27, the address=0x117d0238
- Floating-point types: float32 and Float64
The initial default value is: 0.0.
Note: There is no type of float and you cannot use = = and! = When comparing two floating-point numbers
Format the output when the format string is:%g,%f corresponds to a pointer to a floating-point type; the%e corresponds to the scientific counter-prosecute output;%N.MG is used to specify the decimal output.
Instance code:
Output Result:
- F32 = 2.054545, f64=4.054544925689697, default=0
- F32 = 2.05
- Plural type:
Complex64: Both real and imaginary are 32 bits
COMPLEX128: Both real and imaginary are 64 bits
Real (c): Get the real number part
Imag (c): Obtaining imaginary parts
Format the output when the format string is:%v, you can also use%f to output real and imaginary parts.
Instance code:
Output Result:
- The com1= (2+3i), the com2= (3+4i)
- The com2= (5+7i)
- The real of com2=5, the imag of com2=7
Character type:
- Strictly speaking, there is no such type in go, it is a special integer type.
- It corresponds to the uint8 type, which corresponds to the traditional ASCII code, which accounts for 1byte.
- Unicode (UTF-8) encoding is also supported, so it may point to multiple bytes, known as Unicode code points or runes. At this point it corresponds to the number type of the Int32.
- Enclosed in single quotation marks.
- Unicode characters are usually represented in 16-binary form (\u+4 or \u+8)
- Format the output when the format string is:%c;%v or%d displays the corresponding integer value;%u output: u+hhhh
String type:
- A string of UTF-8 encoded characters (which may account for 1~4byte)----characters in Java are 2bytes.
- Enclosed in double quotes, only in a separate line. (interpreted string)
- Enclosed in anti-quotes, you can span multiple lines. (Raw String)
- Note: Go all the code is UTF-8 format, so there is no character encoding and decoding.
- It is an immutable value type, so you cannot modify the string directly.
Pointer type:
- is 4byte in size.
- Each data type has a corresponding pointer type.
- Declaring a method is similar to the declaration of a pointer in C: *type.
4 operator
- Logical operators:
- With: &&, or: | |, non:!.
- ==,!=,<,>,<=,>=
- Bitwise operators:
- can only work on variables of type int.
- Bitwise vs: &, bitwise OR: |, bitwise XOR: ^
- Shift left: <<, move right:>>. (Vacancy 0)
- Arithmetic operators:
- +,-,*,/.
- Modulus:%
- Simplified operation:-=,*=, + =,%=
- + +,--。 (only after the number variable, not at the beginning, which is not the same as C,java).
- Priority: (7-1: From high to low)
Global variables
Outside of the Func
Local variables
Within the Func
Source file Template
package main// 程序所属包import "fmt"//导入依赖包const NAME string = "SoulMO"//定义常量var a string = "Golang学习"//声明全局变量及赋值type GoLearntInt int//一般类型声明type Learn struct{}//结构体声明type ILearn interface { }//声明接口func GoLearn() { fmt.Println(a,"learn Golang")}//定义函数func main() { GoLearn(); fmt.Println(a,"Hello ~")}// main函数
Basic syntax
1, Package
- Package is the most basic distribution unit and the embodiment of dependency in engineering management
- Each Golang source code file has a package declaration that indicates the source code file belongs to the code bundle
- To generate the Golang executable, you must have the package of main, and the package must have main ()
- Only one package can exist under the same path, and a single one may consist of multiple source code files
Package name is the current folder name to avoid confusion
2,import
- Import is imported according to order
Function name must start with uppercase
3 Type 0 Value
Value type defaults to 0
Boolean type defaults to False
string defaults to an empty string
3, parameter declaration initialization and assignment
Variable declaration format
var <变量名称> [变量类型]
Variable assignment format
<变量名称> = <值,表达式,函数等>
Variable declaration and assignment format
var <变量名称> [变量类型] = <值,表达式,函数等>
Group Declaration format
var( i int j float32 name string)
Single-line declaration of multiple variables and assignments
var a, b, c int = 1, 2, 3Ora, b := 1, 2
Global variable declaration must be var, local variable can be omitted
Special variable Underline "_";
Assigning a value to "_" means that the value is destroyed, and the next operation will not be able to invoke the value
The go-type conversion must be explicit
Type conversions can only occur between two compatible types
Type conversion format<变量名称> [:]= <目标类型>
A variable that starts with a capital letter is a common variable that can be called by another package read
A variable is a private variable that starts with a lowercase letter and can only be used by the current package