This is a creation in Article, where the information may have evolved or changed.
What is go?
Go is a compiled system programming language for concurrent support and garbage collection . Designed to create a programming language with a good balance between high-performance and dynamic languages in statically compiled languages.
Official website: https://golang.org
Go features
- Type safety and memory security
- High concurrency with very intuitive and very low-generation solutions
- Efficient garbage Collection
- Fast Compilation
- UTF-8 Support
The existence value of Go
Go on Google: a language design for software engineering purposes
Installation
Download Link:
Http://www.golangtc.com/download
Easy to install, just click Next to
Once installed, the software will automatically help us configure the path environment.
After installing Okey, you can view the Go language compilation parameters by entering go env at the command prompt.
Gopath settings
What's the use of Gopath?
Gopath is a directory for go to find a list of directories for a package. When using the import "package name", if you cannot find it in the Goroot (installation directory), you should turn to the Gopath to find it.
Gopath can effectively prevent third-party packages from polluting the pkg and SRC files of the core go.
The $GOPATH directory convention has three subdirectories:
-src store source code (e.g.. Go. C. h, etc.)
-Files generated by PKG after compilation (e.g.,. a)
-executable file generated after bin compilation
Set path naming with set Gopath
Of course, under Windows can also be set by environment variables
Go common commands
Go get
Get Remote Package
Go Run
Compiles the listed file and generates the executable file and executes it. Note that you can only use the main package, otherwise the Go run:cannot run Non-main pack error will occur.
Go bulid
Compile the package, if the main package generates an executable file in the current directory, the other packages do not generate. a files;
Go FMT
Format Source code
Go Install
Compile the package while copying the results to GOP AT H /bIN, Gopath/pkg such as the corresponding directory;
Go Test
Run the test file
Go Doc
View Documents
Use of help documents
The corresponding command via go doc+
Direct access to the official website: https://golang.org
If the network speed is busy, you can enter go doc -http=:8081 it at the command line
In this way, you can access it locally
Data types in the Go language
- boolean bool
Length: 1 bytes
Value range: true, False
You cannot use a number to represent true, false
- Integral type Int/uint
Platform if the 32-bit is 32, if the platform is 64-bit, then 64-bit, determined by the platform.
The integer range can be defined by the following number
Int8/uint8 represents a 8-bit integral type
Range -128~127/0~255
And of course, int16/uint16, Int32/uint32, Int64/uint64.
- Character type Byte (Uint8 alias)
- Floating-point Float32/float64
Length 4/8 bytes
- Plural: complex64/complex128
Length 8/16 bytes
- Other types
Array, struct, string
- Reference type
Slice, map, chan
- Interface type
Inteface
- Function type Func
Declaration of a variable
Variable declaration format
var < variable name > < variable type >
Assigning values to variables
< variable names >=< expressions >
Assigning values at the same time as declarations
var < variable name > [< variable type >]=< expression >
varint8//变量声明a=16//变量赋值varint =15//同时进行变量声明和赋值varint =15//同时进行变量声明和赋值d:=15//变量声明和赋值最简写法
Instance
Under D:\goProject directory structure
——-bin
——-SRC
———— –main
——————— Index.go
——————— Trace.go
——-Pkg
Index.go files are as follows
//命名当前程序的包名package main//通过import导入其他包import"trace"// 定义常量func main(){ hello() trace.Show("hello,world")}//首字母小写,表示外部不能调用func hello(){ trace.Show("hello,lidequan")}
Trace.go
package traceimport"fmt"//首字母大写,表示外部可以调用funcstring) { fmt.Println("a.go is package mufunc.") fmt.Println(str)}
Enter a name in the command prompt go install
You can see the exe executable file generated under the Bin directory to generate the package file under Pkg
If you want to run directly, you can also go to +index.go
Precautions
1. Only one package can exist in each subdirectory, or the compilation will be error-
Case-sensitive issues
In the go language, use case to determine whether constants, variables, types, interfaces, structures, or functions can be called externally. If the function name of the first letter lowercase is private, external can not be called, the first letter is public, the outer can be called.