当前位置:网站首页>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
边栏推荐
- 15million employees are easy to manage, and the cloud native database gaussdb makes HR office more efficient
- 紫光展锐完成全球首个 5G R17 IoT NTN 卫星物联网上星实测
- Development and construction of DFI ecological NFT mobile mining system
- 如何让你的小游戏适配不同尺寸的手机屏幕
- Ziguang zhanrui completed the first 5g R17 IOT NTN satellite on the Internet of things in the world
- 软件客户端数字签名一定要申请代码签名证书吗?
- 【历史上的今天】7 月 4 日:第一本电子书问世;磁条卡的发明者出生;掌上电脑先驱诞生
- 华为云云商店首页 Banner 资源位申请
- Win11共享文件打不开怎么办?Win11共享文件打不开的解决方法
- 针对深度学习的“失忆症”,科学家提出基于相似性加权交错学习,登上PNAS
猜你喜欢

同事的接口文档我每次看着就头大,毛病多多。。。

Win11亮度被锁定怎么办?Win11亮度被锁定的解决方法

华为nova 10系列支持应用安全检测功能 筑牢手机安全防火墙

Neural network IOT platform construction (IOT platform construction practical tutorial)

Win11U盘拒绝访问怎么办?Win11U盘拒绝访问的有效解决方法

B2B mall system development of electronic components: an example of enabling enterprises to build standardized purchase, sale and inventory processes

C # better operation mongodb database

Crystal optoelectronics: ar-hud products of Chang'an dark blue sl03 are supplied by the company

Development and construction of DFI ecological NFT mobile mining system
一文搞懂Go语言中文件的读写与创建
随机推荐
The company needs to be monitored. How do ZABBIX and Prometheus choose? That's the right choice!
What financial products can you buy with a deposit of 100000 yuan?
Flet教程之 06 TextButton基础入门(教程含源码)
Installation and use of VMware Tools and open VM tools: solve the problems of incomplete screen and unable to transfer files of virtual machines
哈希(Hash)竞猜游戏系统开发功能分析及源码
华为nova 10系列支持应用安全检测功能 筑牢手机安全防火墙
Length of the longest integrable subarray
Decryption function calculates "task state and lifecycle management" of asynchronous task capability
[ismb2022 tutorial] the picture shows the precision medicine of learning. Marinka zitnik, Harvard University, keynote speaker, with 87 ppt
Employment prospects of neural network Internet of things application technology [welcome to add]
Development and construction of DFI ecological NFT mobile mining system
Dark horse programmer - software testing - stage 07 2-linux and database -09-24-linux command learning steps, wildcards, absolute paths, relative paths, common commands for files and directories, file
强化学习-学习笔记2 | 价值学习
Lingyun going to sea | 10 jump &huawei cloud: jointly help Africa's inclusive financial services
2022 Health Exhibition, Beijing Health Expo, China Health Exhibition, great health exhibition November 13
B2B mall system development of electronic components: an example of enabling enterprises to build standardized purchase, sale and inventory processes
华为云云商店首页 Banner 资源位申请
NLP、视觉、芯片...AI重点方向发展几何?青源会展望报告发布[附下载]
ICML 2022 | Meta提出鲁棒的多目标贝叶斯优化方法,有效应对输入噪声
The problem of the maximum difference between the left and right maxima