目录
- goLang安装
- 常用命令
- helloworld 测试代码
- IDE介绍(安装忽略)
golang安装
下载解压
wget https://golang.google.cn/dl/go1.20.1.linux-amd64.tar.gz
tar zxvf go1.20.linux-amd64.tar.gz -C /usr/local
#设置环增变量 - module模式
echo 'export GOROOT=/usr/local/go' >> /etc/profile
echo 'export GOPATH=/usr/local/go/workspace' >> /etc/profile
echo 'export GOBIN=$GOROOT/bin' >> /etc/profile
echo 'export PATH=$GOBIN:$GOPATH/bin:$PATH' >> /etc/profile
echo 'export GO111MODULE=on' >> /etc/profile
环境生效
source /etc/profile
开启代理
go env -w GO111MODULE=on
go env -w GOPROXY=https://goproxy.cn,direct
测试安装结果-如下信息说明安装成功
go version
---
go version go1.20 ...
查看go环境变量
--------------
...
set GOMODCACHE=C:\Users\carmen-x13\go\pkg\mod
...
set GOPATH=C:\Users\carmen-x13\go
...
set GOPROXY=https://proxy.golang.org,direct
set GOROOT=C:\Program Files\Go
set GOSUMDB=sum.golang.org
...
set GOTOOLDIR=C:\Program Files\Go\pkg\tool\windows_amd64
go 的常用命令
- go help 查看帮助
go help
----------------------------
Go is a tool for managing Go source code.
Usage: go [arguments]
The commands are:
bug start a bug report: 用于调试
build compile packages and dependencies: 编译
clean remove object files and cached files: 删除
doc show documentation for package or symbol: 在线文档查看
env print Go environment information: 环境变量
fix update packages to use new APIs:
fmt gofmt (reformat) package sources: 代码格式化
generate generate Go files by processing source
get add dependencies to current module and install them
install compile and install packages and dependencies
list list packages or modules
mod module maintenance
work workspace maintenance
run compile and run Go program
test test packages
tool run specified go tool
version print Go version
vet report likely mistakes in packages
- go doc 在线查看fmt包的帮助文档,比美linux下的man
go doc fmt
---
package fmt // import "fmt"
Package fmt implements formatted I/O with functions analogous to C's printf and
scanf. The format 'verbs' are derived from C's but are simpler.
...
编写测试
vim hello.go
-- 1. 创建目录
mkdir ~/gowork/hello
#usr/local/gowork
-- 2. 项目初始化
cd ~/gowork
go mod init hello
#要先开启go module模式
#初始化时,需要指定模块名
#在根目录下创建go文件
-- 3.vim hello.go
package main
import (
"fmt"
)
func main() {
fmt.Println("hello world")
}
-- 4.运行
1.方法一
go build hello.go #编译
./hello #运行
2.方法二
#编译+运行
go run build.go #运行
三个关键字:
package 定义包名
import 导⼊包名
func 函数定义关键字
IDE安装
- LiteIDE -
- GoLang - 需要破解
- VsCode - 对应插件
- ...
安装过程网上很多指南,可自搜