当前位置:网站首页>Go language notes (2) some simple applications of go

Go language notes (2) some simple applications of go

2022-07-04 20:48:00 fiveym

first Go Program

Write code
Create a new file ~/code/example.go , write in

//  Declare the package where the file is located , If it is the main program, it is main 
package main 

//  Import library ,fmt Used to process standard input and output  
import "fmt" 

// main  Function is the entry to the whole program ,main  The package name of the function must also be  `main` 
func main() {
     
//  call fmt Bag Println Method , Output information on the screen  
fmt.Println("Hello World!") 
}
package main
     The statement  main.go  The bag where it is ,Go  Language uses packages to organize code .
     Generally, a folder is a package , The type or method can be exposed in the package for use by other packages .

import “fmt”
    fmt  yes  Go  A standard library of languages / package , Used to process standard input and output .

func main
    main  Function is the entry to the whole program ,main  The package name of the function must also be  main .
    !! The main package must contain a main function !!

fmt.Println(“Hello World!”)
     call  fmt  Bag  Println  Method , Print out  “Hello World!”

 Other knowledge points :
      A line represents the end of a statement , Multiple statements are written on the same line , They have to use  ;  Artificial distinction 
 notes :
     Single-line comments : //
     Multiline comment :/* sth */
     identifier : Same as python Identifier rule 

Run code

Direct operation
perform go run example.go , Will be output

$ go run example.go 
Hello World!
/* If forced enabled  Go Modules  Mechanism ,  That is, the environment variable is set  GO111MODULE=on,  You need to initialize the module first  go mod init hello  Otherwise, a mistake will be reported :go: cannot find main module; see ‘go help modules*/

Compile and then execute
Compile into binary executable program ,build after ,
You will find that an executable with the same name will be generated in the same directory ( No extension )

$ go build example.go

Execute the program

./example

Go Language command

Go It's a compiled language ,Go The tool chain of language converts the source code and its dependencies into the machine instructions of the computer ( Translation notes : Static compilation ).
$ go run example.go Hello World! $ go build example.go ./example
Go All provide a single command in a single language go call , go The command has a series of subcommands .
The simplest subcommand is run. This command compiles one or more to .go At the end of the source file , Link library files , And run the finally generated executable
file .
Go Language native support Unicode, It can handle text in any language in the world .

build Sons command : Compile into executable binary files :Windows The executable file generated under the system is helloworld.exe, increase
了 .exe Suffix name )

$ go build example.go 
$ ./example 
Hello World!

run Sons command : Run the source code

$ go run example.go 
Hello World!

Go Language configuration

Configure the installation source

//  Set up module management model  
$ go env -w GO111MODULE=on 
// Set download source  
$ go env -w GOPROXY=https://goproxy.cn

Try to configure Go project

The code is as follows :

example/proj.go
package main import ( 
    "fmt"
    "github.com/jinzhu/configor" 
)
func main() {
     
    fmt.Println("vim-go", configor.Config{
    }) 
}

Initialize project : It mainly solves the problem of package management

#  initialization  
go mod init account 
#  Solve the problem of dependence ( Check , Delete the wrong or unused modules, Did you download it download Of packagego mod tidy

Operation method 1 : Direct operation

# go run proj.go 
vim-go {
     false false false false 0s <nil> false}

Operation method II : Compile to executable

# go build proj.go 
# proj Files can be moved to any directory to run directly , There will be no more dependency problems  
# ./proj 
# go run proj.go
原网站

版权声明
本文为[fiveym]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/185/202207041902540985.html