Environment
Go Download Install
Recommended go to Golang Chinese community download go compilation organ party download
A fool-type installation can
Open cmd after installation, enter go version the successful output version number to indicate successful installation
Go Environment variables
A question about the environment variables of Go Seaconch just suddenly thought of the more appropriate way to describe
1. Goroot
Goroot is often described as the path to the Go compiler
Goroot do not need us to match , generally the path chosen when installing the GO compiler is the goroot path
Seaconch added: When you import a package, the native pkg is also referenced by Goroot.
So the students who have used NPM can understand that: Global package path
2. Gopath
Gopath is often described as the Go Source (engineering) path
Gopath need us to manually configure , of course, if just write a demo practiced hand, not worthy of it
Seaconch thinks Gopath is a bit like NPM's local package path
3. Supplement
This is not to say that you cannot introduce packages under either of these two directories, for example we can pass the . /pkg 's way of introducing and using our own packages is no problem, and as long as the relative path relationship remains the same, the copy can be run anywhere, and the downside is not to execute the go install command
Hello World Scout
Create a new Notepad in any location
Enter the following code
package mainimport "fmt"func main () { fmt.Println("seaconch.")}
Save the file as a.go Note the suffix name to ensure that it is not a. txt
Run the cmd input on the path where the A.go is locatedgo run a.go
You can see that the seaconch has been successfully exported.
输出中文的小伙伴现在还不要着急,因为目前还不是 utf-8 编码
Common commands
Go Run xxx.go
Debug artifacts without a build to execute code directly; In special cases, batch run is required, for example go run main1.go main2.go
注: main1.go 与 main2.go 同包 并属于 extension 关系
Go Build Xxx.go
Compile the source code, generate executable programs, and in special cases require batch compilation, for examplego build main1.go main2.go
Go Install
Compile the build project executable file, which is generated by default in the bin directory in the gopath directory
Variables and data types
Variable
Define VARIABLE:VAR variable name type
Give me a chestnut.
- var _name string
- VAR (
- _name string
- _age int
- )
Simple define: Variable name: = Expression
This method applies only to the inside of the function
Give me a chestnut:
- _name: = "Seaconch"
- _name, _age: = "Seaconch", 99
注:变量名称区分大小写
Characteristics
In the Go language, there are three distinct features in the operation of a variable.
:=This method only applies to variables that have not been define , and can only be used inside the func
x, y := y, xThe classic variable exchange is a very simple issue in the Go language, just like this.
_Because the go language, multiple return values are particularly common, when we only need multiple return values of the individual values, you can use the name, _: = Method () to discard the second return value, a similar way to achieve the desired effect
Constant
Constants need to have an initial value
Define
Give me a chestnut:
Const _PI float64 = 3.14 / const _version String = "v1.0"
Multi-assigned value
Const (
A = 1BC = 2D
)
In multi-assignment cases, constants that are not assigned by default use the value of the previous constant, so B = 1, D = 2
To represent an enumeration by using Iota :
const ( sunday = iota monday tuesday wednesday= 88 thursday friday = iota saturday)
Sunday-0
Monday-1
Tuesday-2
Wednesday-88
Thursday-88
Friday-5
Saturday-6
Can see the role of Iota, Iota is a bit like a counter , seaconch, use this small example to illustrate its use
Integer
int shaping, based on CPU & IDE Decision 64/32
UINT unsigned int
byte = = Uint8 can be understood as type aliases
Rune = = Int32 can be understood as type aliases
Floating point
Float32/float64 accuracy is different
Plural skip
Boolean type
The Boolean type has two points that need to be mainly:
- The default value is False
- Golang 1 with true 0 is not comparable to false
String
To intercept a string:
_str := "hello seaconch." fmt.Println(_str[:]) -> hello seaconch. fmt.Println(_str[7:]) -> eaconch. fmt.Println(_str[:8]) -> hello se fmt.Println(_str[7:8]) -> e
Look at the code knowing ~
7 Index of <= target string < 8
All right, let's get here today.