One, the first go program, Hello World!!!
Create the Learngo directory, create SRC, and finally write the Hello.go file
Package Mainimport ( "FMT") func main () { fmt. Printf ("Hello World!!!")}
From the first go program, we can start to understand the program structure of the Go language
1,package Declaration of the Go language package,
2,import Importing Packages
3,func main () {} Go language function
Some places to be aware of in the Go language
(1) Packeage main means that the package can be compiled into an executable file (the difference between the executable file and the library is that after the executable file is compiled, it can be executed on its own, if the library file is compiled and only one Lib library can be produced, the Lib library can only be called by other executables)
(2) Go language imported package or defined variables, functions, etc., need to be used in the program, if not used, the program compiles will error. This is also a minimalist approach to programming in the Go language
After writing the first go program, how do I run it? Locate the go file you wrote, and then execute go run src/hello.go (Quick execute go file)
Second, the general use of commands in the Go language
(1) Go language environment variable view, execute Go env under Windows to list all environment variables of Go language
The most important of these variables is Gopath, all the source code, the program is placed in the Gopath, Gopath path is best only set a
In addition, the go language has a cross-platform, under Windows to write a go program, after the completion of the compilation, Linux next can be executed
(2) Go Build command is used to compile the Go program, after the compilation will generate an. exe binary file, which can be executed wherever it is, go build Project/webdev/main, in the compilation command, only need to write src after the file path OK
(3) The Go Install command is to install the executable file to the Bin directory go install Project/webdev/main, the first time you perform go install Gopath will automatically generate a bin directory, this bin Directory holds all compiled executable programs
(4). \ to run the compiled executable. exe file
(5) Go test performs unit tests, the source file name is appended with _test, the function is defined in the file, and the function defines the Func testadd (T *testing. T), the go language does not compile the test file into it in the compilation
Test code example
Package Calcimport ( "Testing" #导入testing module) func testadd (t *testing. T) { #test function definition var sum int sum = ADD (5, 6) if sum! = Ten { t.fatalf ("Add is not right, sum:%v expected : one ", sum) #定义出错时输出 } T.logf (" Add is OK ") #打印正确日志}
(6) Go FMT format all the source code, the format of the source code to standardize, go fmt-w./* Can be formatted for all files in the current directory
Third, write a custom go language pack
(1) Create a new directory, such as Calc
Package Cale #声明这个包的名字, preferably the same as the directory you created. Func Add (a int, b int) int { #本句中, the first two int is the data type of the declaration parameter, and the latter int is the type of the return value #go Language is a strongly typed language, variables typically require a specified type return a+b}
(2) After the new go package, how to boot into the other code, (note that the import needs to import the entire package, you can not import a separate. Go file in the package)
Package Mainimport ( "FMT" Cale "Project/webdev/calc" #导入自定义包 (the path after which the import path is SRC)
#路径前面的cale为包设置别名) Func main () { x: = Calc. ADD (#go) The invocation of the function in the package, the name of the package. Function Import FMT. Printf ("x:%d". X)}
(3) Go language in the organization of the package, it should be noted that in the same directory, all go source files can only have one, that is, the package name can only have one, if there are multiple, there will be compile error, in addition, the package name is main and has func main This package is executable after compiling.
Four, the definition of the Go language multithreading
implementation based on CSP model
Func Calc () {
Large number of calculations
}
Func Main () {
Go Calc () #在函数前面添加go statement
}
Five, the Go Language Program communication method----only the pipeline
(1) piping, similar to pipe in Unix/linux
(2) communication via channel between multiple Goroute
(3) Support for any type in the Go language
Func Main () { Pipe: = Make (chan int,3) #pip: = define PIP variable, make allocate space pipe <-1 #chan pipe, int is the data type in the pipeline , 3 for pipe memory size pipe <-2 #<-deposit data
var a int
A <-pipe #读取管道中的数据}
Six, go language returns multiple values
The type of return value that needs to be defined in the go language when the function is defined
Func Calc (a int, b int) (int,int) { # (Int,int) defines the type of the return value sum: = a+b avg : = (a+b)/2 return sum, avg #返回多个值}
Go Language Learning-Basic (1)