当前位置:网站首页>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 package)
go 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
边栏推荐
- Cdga | six principles that data governance has to adhere to
- Practice examples to understand JS strong cache negotiation cache
- In operation (i.e. included in) usage of SSRs filter
- WinCC7.5 SP1如何通过交叉索引来寻找变量及其位置?
- go笔记(1)go语言介绍以及特点
- Win11无法将值写入注册表项如何解决?
- word中插入圖片後,圖片上方有一空行,且删除後布局變亂
- AP8022开关电源小家电ACDC芯片离线式开关电源IC
- Lingyun going to sea | 10 jump &huawei cloud: jointly help Africa's inclusive financial services
- Jiuqi ny8b062d MCU specification /datasheet
猜你喜欢
托管式服务网络:云原生时代的应用体系架构进化
Qt五子棋人机对战画棋子之QPainter的使用误区总结
Fleet tutorial 08 introduction to AppBar toolbar Basics (tutorial includes source code)
ICML 2022 | meta proposes a robust multi-objective Bayesian optimization method to effectively deal with input noise
Alibaba testers use UI automated testing to achieve element positioning
科普达人丨一文看懂阿里云的秘密武器“神龙架构”
NLP, vision, chip What is the development direction of AI? Release of the outlook report of Qingyuan Association [download attached]
WinCC7.5 SP1如何通过交叉索引来寻找变量及其位置?
What if the WiFi of win11 system always drops? Solution of WiFi total drop in win11 system
How to adapt your games to different sizes of mobile screen
随机推荐
What if the WiFi of win11 system always drops? Solution of WiFi total drop in win11 system
go语言笔记(4)go常用管理命令
Summary of the mistakes in the use of qpainter in QT gobang man-machine game
PermissionError: [Errno 13] Permission denied: ‘data.csv‘
Common verification rules of form components -1 (continuously updating ~)
Advantages of semantic tags and block level inline elements
The problem of the maximum difference between the left and right maxima
Qt编写物联网管理平台38-多种数据库支持
js 闭包
Ziguang zhanrui completed the first 5g R17 IOT NTN satellite on the Internet of things in the world
Practical examples of node strong cache and negotiation cache
Win11共享文件打不开怎么办?Win11共享文件打不开的解决方法
Integretee integrates into Moonriver through xcm, bringing enterprise class privacy solutions to its ecosystem
科普达人丨一文看懂阿里云的秘密武器“神龙架构”
Huawei cloud store homepage banner resource bit application
What if the brightness of win11 is locked? Solution to win11 brightness locking
Understand Alibaba cloud's secret weapon "dragon architecture" in the article "science popularization talent"
更强的 JsonPath 兼容性及性能测试之2022版(Snack3,Fastjson2,jayway.jsonpath)
Idea restore default shortcut key
[ismb2022 tutorial] the picture shows the precision medicine of learning. Marinka zitnik, Harvard University, keynote speaker, with 87 ppt