当前位置:网站首页>go语言笔记(2)go一些简单运用
go语言笔记(2)go一些简单运用
2022-07-04 19:03:00 【fiveym】
第一个Go程序
编写代码
新建一个文件 ~/code/example.go ,写入
// 声明该文件所在包,如果是主程序就是main
package main
// 导入库,fmt用来处理标准输入输出
import "fmt"
// main 函数是整个程序的入口,main 函数所在的包名也必须为 `main`
func main() {
// 调用fmt包的Println方法,在屏幕输出信息
fmt.Println("Hello World!")
}
package main
声明了 main.go 所在的包,Go 语言中使用包来组织代码。
一般一个文件夹即一个包,包内可以暴露类型或方法供其他包使用。
import “fmt”
fmt 是 Go 语言的一个标准库/包,用来处理标准输入输出。
func main
main 函数是整个程序的入口,main 函数所在的包名也必须为 main 。
!!主包中必须包含一个main函数!!
fmt.Println(“Hello World!”)
调用 fmt 包的 Println 方法,打印出 “Hello World!”
其他知识点:
一行代表一个语句结束,多个语句写在同一行,它们则必须使用 ; 人为区分
注释:
单行注释: //
多行注释:/* sth */
标识符:同python标识符规则
运行代码
直接运行
执行 go run example.go ,将会输出
$ go run example.go
Hello World!
/*如果强制启用了 Go Modules 机制, 即环境变量中设置了 GO111MODULE=on, 则需要先初始化模块 go mod init hello 否则会报错误:go: cannot find main module; see ‘go help modules*/
先编译再执行
编译成二进制可执行程序,build后,
你会发现在同级目录下会生成一个同名的可执行文件(无扩展名)
$ go build example.go
执行该程序
./example
Go语言命令
Go是一门编译型语言,Go语言的工具链将源代码及其依赖转换成计算机的机器指令(译注:静态编译)。
$ go run example.go Hello World! $ go build example.go ./example
Go语言提供的工具都通过一个单独的命令 go 调用, go 命令有一系列子命令。
最简单的一个子命令就是run。这个命令编译一个或多个以.go结尾的源文件,链接库文件,并运行最终生成的可执行
文件。
Go语言原生支持Unicode,它可以处理全世界任何语言的文本。
build子命令:编译成可执行二进制文件译注:Windows系统下生成的可执行文件是helloworld.exe,增加
了.exe后缀名)
$ go build example.go
$ ./example
Hello World!
run子命令:运行源代码
$ go run example.go
Hello World!
Go语言配置
配置安装源
// 设置module管理模式
$ go env -w GO111MODULE=on
//设置下载源
$ go env -w GOPROXY=https://goproxy.cn
尝试配置Go项目
代码如下:
example/proj.go
package main import (
"fmt"
"github.com/jinzhu/configor"
)
func main() {
fmt.Println("vim-go", configor.Config{
})
}
初始化项目:主要解决包管理问题
# 初始化
go mod init account
# 解决依赖问题(检查,删除错误或者不使用的modules,下载没download的package)
go mod tidy
运行方法一:直接运行
# go run proj.go
vim-go {
false false false false 0s <nil> false}
运行方法二:编译成可执行文件
# go build proj.go
# proj文件移到任何目录都可以直接运行,不会再有依赖问题
# ./proj
# go run proj.go
边栏推荐
- Pytoch learning (4)
- Practice examples to understand JS strong cache negotiation cache
- [graduation season] green ant new fermented grains wine, red mud small stove. If it snows late, can you drink a cup?
- [ismb2022 tutorial] the picture shows the precision medicine of learning. Marinka zitnik, Harvard University, keynote speaker, with 87 ppt
- 应用实践 | 蜀海供应链基于 Apache Doris 的数据中台建设
- Is it necessary to apply for code signing certificate for software client digital signature?
- Write it down once Net analysis of thread burst height of an industrial control data acquisition platform
- QT writing the Internet of things management platform 38- multiple database support
- In operation (i.e. included in) usage of SSRs filter
- [Beijing Xunwei] i.mx6ull development board porting Debian file system
猜你喜欢
Optimization cases of complex factor calculation: deep imbalance, buying and selling pressure index, volatility calculation
Form组件常用校验规则-1(持续更新中~)
Pointnext: review pointnet through improved model training and scaling strategies++
Application practice | Shuhai supply chain construction of data center based on Apache Doris
Understand the reading, writing and creation of files in go language
紫光展锐完成全球首个 5G R17 IoT NTN 卫星物联网上星实测
Process of manually encrypt the mass-producing firmware and programming ESP devices
Key rendering paths for performance optimization
【历史上的今天】7 月 4 日:第一本电子书问世;磁条卡的发明者出生;掌上电脑先驱诞生
Selected review | machine learning technology for Cataract Classification / classification
随机推荐
Data set division
为什么最大速度是光速
Pytoch learning (4)
New wizard effect used by BCG
[Beijing Xunwei] i.mx6ull development board porting Debian file system
Pointnext: review pointnet through improved model training and scaling strategies++
Actual combat simulation │ JWT login authentication
The company needs to be monitored. How do ZABBIX and Prometheus choose? That's the right choice!
Kotlin condition control
Selected review | machine learning technology for Cataract Classification / classification
如何让你的小游戏适配不同尺寸的手机屏幕
Six stones programming: about code, there are six triumphs
Win11系统wifi总掉线怎么办?Win11系统wifi总掉线的解决方法
Dark horse programmer - software testing - stage 08 2-linux and database-23-30-process port related, modify file permissions, obtain port number information, program and process related operations, Li
Detailed explanation of Audi EDI invoice message
node强缓存和协商缓存实战示例
上线首月,这家露营地游客好评率高达99.9%!他是怎么做到的?
易周金融 | Q1保险行业活跃人数8688.67万人 19家支付机构牌照被注销
Small hair cat Internet of things platform construction and application model
What does the neural network Internet of things mean? Popular explanation