Introduction to Go
Go is Golang. Go is a statically strongly typed language (cannot do calculations between types). It is a compiled language that is different from analytical languages.
Go language features
Cross-platform compiled language
Grammar is close to C language
Channel (channel), slice (slice), concurrent (routine)
Has a garbage collection mechanism
Supports object-oriented and process-oriented programming models
One download address
The download address of the installation package is: https://golang.org/dl/ . Installation package (similar to python interpreter)
If you can't open it, you can use this address: https://golang.google.cn/dl/ .
# Go Version View go version # go env view go environment variables GO111MODULE = # no value or off, that there is no open go mode mode, use is gopath mode GOROOT = c: \ go # installation path go development kits, confirming whether for good GOPATH = C: \ the Users \ Administrator \ go # code to be placed path, whether to confirm the good, you go after the code are placed in this folder # anywhere in knocking go, have this command, you need to c : \ go \ binjoin environment variable # go mode mode: the code can be placed in any path
# 1 Whether you use goland or other ide, the path (code) of the new project must be placed in the src folder under the gopath path (if not manually created) # 2 There will be three folders under the gopath path: src (put code ), Pkg (compilation process produces intermediate things), bin (compiled executable file) # 3 Create a project, create a go file under the project, and give it a name # 4 Execute the code (compiled language, first compile and then execution) compilation: go build xx.go -> compiled into an executable file in the current platform, named xx execution: xx or. / xx Windows: in the executable path xx.exe # 5 Go command - Go Build Compile - go install installation, put the compiled executable file under the bin path - go fmt: code formatting - go run xx.go compile and execute # 6 execute in golang right click, select, run
Basic use of Go
Basic syntax (print hello world)
/ * Print hello world * / // Single-line comments in go language use / * Multi-line comments Multi-line comments * / // ctrl + / Can quickly comment and uncomment (goland) package main // Declare the main package, each The first line of the go code must write this import " fmt " // Import fmt package analogy import python os os.path func main () { // Declare the content of the main function body, put it in curly brackets // goland prompt The a ... formal parameter is a fmt.Println ( " hello world " ) // output helloworld in the console ---> print ( " hello world " ) fmt.Println ( " hello world" ) } // The program's execution entry is the main function under the main package // compiled language, all have an entry ---- " Compared with a py file in python is a main function // a go project can only have one main function
Definition and use of variables
package main import " fmt " func main () { // 1. Method one: basic definition // var keyword variable name variable type = variable value // var age int = 18 // define and assign // var age int / / Definition // age = 18 // Assignment // 2. Method 2: Type derivation (no need to add type) // var age = 18 // var age // Wrong, when the variable is defined, the type must be fixed // age = 18 // 3. Method three: abbreviated statement (colon and equal sign are together) // age: = 18 // Printf,% T means type //fmt.Printf ( " The type of age is:% T, The value is:% d \ n " , age, age) // fmt.Println (age) // 4.Method four, define multiple variables at the same time // var a, b, c int = 1,2,3 // var a, b, c = 1,2,3 // deformation (can understand, it means that the definition is OK ) // var ( // a = 19 // b = " lqz " // c = " Male " // ) // fmt.Println (a, b, c) // 5. Variables need to be defined before use // a: = 10 // fmt.Println (a) // 6. Variables cannot be defined repeatedly var a int = 18 // var a = 19 // Repetitively defined error /// Simple special declarations (before the colon, at least There is an undefined variable that will not report an error) a, b: = 17,18 fmt.Println (a, b) // 7The variable type is fixed / * NOTE: 1 . After the variable definition must be used, otherwise an error 2 The packet is introduced, to be used, without using a given 3 to define reuse 4 not redefine 5 type fixing * / }
Definition and use of constants
/ * Constant: A constant and constant amount. It is recommended that constants be written in all capitals . The values that will not change during program operation, such as the connection address of the database, port number * / package main func main () { // 1. const keyword Constant name Constant type = value // 2. Constants can be defined without use, but can not be repeated, the type can not be changed // const age int = 19 // 3. Type can be omitted // const age = 99 //fmt.Printf ( " % T " , age) // 4. Multiple constants can be defined at the same time // const ( // AGE = 19 // NAME = " lqz " // SEX = " Male " // ) // fmt.Println ( AGE, NAME, SEX) // 5.Can't change the constant // const AGE = 99 // const AGE = 18 error )
Basic data types
/ * Basic data types * / Package main FUNC main () { / * . 1 numeric signed integer :( different lengths, different numbers range indicated) - int: on 32-bit machines is Int32, on 64-bit machines is Int64 -int8: th bit. 8 bits, a byte, plus or minus 2 of the 7 th - 1 range -int16: - 15 +/- 2 power range of 1 - Int32: - Int64: unsigned integer: - uint8: 2 to the 8th power -1 - UInt16: - UInt32 - UInt64 float: - float32: the same accuracy - float64: general with 64 Complex: - Complex Type :( understood better not know) the real and imaginary parts of the other: - byte: uint8 alias - Rune: Int32 alias @ 2 string types: double quotes with the package contents Backticks `` trans Quotation marks can wrap // 3 boolean type true false (lower case) * / // numeric type // var a int = 200 // var a uint8 = -200 // fmt.Println (a) // float // // var a float32 // var b float64 // complex // var a complex64 = 9i + 10 // fmt.Println (a) // string // var name = " "lqz // var name2 = `egon // is // big` // fmt.Println (name) // fmt.Println (name2) // If there are double quotes in double quotes, add \ escape or use backquotes String var name = // " LQZ \" dafds \ "IS " // String = `LQZ var NAME1 " dafds " IS ` // String var = NAME2 " LQZ dafds`is` " // fmt.Println (name) / / fmt.Println (NAME1) // fmt.Println (NAME2) // boolean // var A = BOOL to true // var a =true // a: = true // var b bool = false // fmt.Println (a) // fmt.Println (b) // type conversion (strong type: no operation can be done between types ) // type conversion // var a int = 19 // var b float32 = 18.1 // Float is converted to int type, deprecated directly after the decimal point, not rounded off //fmt.Println(a+ int (b)) // var b float32 = 18.999 / /fmt.Println(int(b)) // The output is 18 // (understand) // var a int = 199 // var b int64 = 199 //// int and int64 are not a type //fmt.Println ( a + int (b)) }