当前位置:网站首页>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
边栏推荐
猜你喜欢

电脑共享打印机拒绝访问要怎么办

Qt编写物联网管理平台38-多种数据库支持
Practice examples to understand JS strong cache negotiation cache
![[today in history] July 4: the first e-book came out; The inventor of magnetic stripe card was born; Palm computer pioneer was born](/img/0b/73f0d98a6db813e54074abe199ed98.png)
[today in history] July 4: the first e-book came out; The inventor of magnetic stripe card was born; Palm computer pioneer was born

FS8B711S14电动红酒开瓶器单片机IC方案开发专用集成IC

Alibaba testers use UI automated testing to achieve element positioning

Qt五子棋人机对战画棋子之QPainter的使用误区总结

Flet教程之 06 TextButton基础入门(教程含源码)

Managed service network: application architecture evolution in the cloud native Era

Flet tutorial 05 outlinedbutton basic introduction (tutorial includes source code)
随机推荐
二叉树的四种遍历方式以及中序后序、前序中序、前序后序、层序创建二叉树【专为力扣刷题而打造】
E-week finance | Q1 the number of active people in the insurance industry was 86.8867 million, and the licenses of 19 Payment institutions were cancelled
[today in history] July 4: the first e-book came out; The inventor of magnetic stripe card was born; Palm computer pioneer was born
Flet tutorial 07 basic introduction to popupmenubutton (tutorial includes source code)
面对同样复杂的测试任务为什么大老很快能梳理解决方案,阿里十年测试工程师道出其中的技巧
QT writing the Internet of things management platform 38- multiple database support
科普达人丨一文看懂阿里云的秘密武器“神龙架构”
NLP、视觉、芯片...AI重点方向发展几何?青源会展望报告发布[附下载]
LeetCode 871. Minimum refueling times
Lingyun going to sea | Wenhua online & Huawei cloud: creating a new solution for smart teaching in Africa
Browser render page pass
漫谈客户端存储技术之Cookie篇
Win11怎么搜索无线显示器?Win11查找无线显示器设备的方法
What should I do if my computer sharing printer refuses access
Lingyun going to sea | Murong Technology & Huawei cloud: creating a model of financial SaaS solutions in Africa
FS4061A升压8.4V充电IC芯片和FS4061B升压12.6V充电IC芯片规格书datasheet
Is it safe for Great Wall Securities to open an account? Stock account opening process online account opening
Taishan Office Technology Lecture: about the order of background (shading and highlighting)
[in-depth learning] review pytoch's 19 loss functions
Record the online bug solving list (unfinished to be continued 7/4)