当前位置:网站首页>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
边栏推荐
- 剑指 Offer II 80-100(持续更新)
- LeetCode 871. Minimum refueling times
- 左右最值最大差问题
- Talking about cookies of client storage technology
- 什么叫内卷?
- ICML 2022 | Meta提出鲁棒的多目标贝叶斯优化方法,有效应对输入噪声
- idea恢复默认快捷键
- What if win11u disk refuses access? An effective solution to win11u disk access denial
- What if the computer page cannot be full screen? The solution of win11 page cannot be full screen
- Practical examples of node strong cache and negotiation cache
猜你喜欢
Selected review | machine learning technology for Cataract Classification / classification
What if the brightness of win11 is locked? Solution to win11 brightness locking
NLP、视觉、芯片...AI重点方向发展几何?青源会展望报告发布[附下载]
Alibaba testers use UI automated testing to achieve element positioning
托管式服务网络:云原生时代的应用体系架构进化
Practice examples to understand JS strong cache negotiation cache
How to solve the problem that win11 cannot write the value to the registry key?
hash 表的概念及应用
What is involution?
Fleet tutorial 08 introduction to AppBar toolbar Basics (tutorial includes source code)
随机推荐
Idea case shortcut
奏响青春的乐章
科普达人丨一文看懂阿里云的秘密武器“神龙架构”
How to adapt your games to different sizes of mobile screen
Flet tutorial 07 basic introduction to popupmenubutton (tutorial includes source code)
太方便了,钉钉上就可完成代码发布审批啦!
Aiming at the "amnesia" of deep learning, scientists proposed that based on similarity weighted interleaved learning, they can board PNAS
Understand the reading, writing and creation of files in go language
Idea plug-in
QT writing the Internet of things management platform 38- multiple database support
LeetCode 7. 整数反转
#夏日挑战赛#带你玩转HarmonyOS多端钢琴演奏
Every time I look at the interface documents of my colleagues, I get confused and have a lot of problems...
idea配置标准注释
[ismb2022 tutorial] the picture shows the precision medicine of learning. Marinka zitnik, Harvard University, keynote speaker, with 87 ppt
[Beijing Xunwei] i.mx6ull development board porting Debian file system
What is involution?
idea插件
一文搞懂Go语言中文件的读写与创建
最长的可整合子数组的长度