0%

Go 环境配置

Go 环境配置

Go 版本管理

Go 版本管理工具 gvm 管理多个版本的 Golang

gvm 安装

  1. 安装 bison

    1
    apt-get install bison
  2. 安装 gvm

    1
    bash < <(curl -s -S -L https://raw.githubusercontent.com/moovweb/gvm/master/binscripts/gvm-installer)
  3. 添加环境变量

    1
    source /root/.gvm/scripts/gvm

gvm 管理多个 go 版本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
root@ubuntu2404:~/.gvm/scripts# gvm help
Usage: gvm [command]

Description:
GVM is the Go Version Manager

Commands:
version - print the gvm version number
get - gets the latest code (for debugging)
use - select a go version to use (--default to set permanently)
diff - view changes to Go root
help - display this usage text
implode - completely remove gvm
install - install go versions
uninstall - uninstall go versions
cross - install go cross compilers
linkthis - link this directory into GOPATH
list - list installed go versions
listall - list available versions
alias - manage go version aliases
pkgset - manage go packages sets
pkgenv - edit the environment for a package set
applymod - apply the go version in go.mod
  • 查看已下载 go 版本

    1
    gvm list
  • 查看可下载 go 版本

    1
    gvm listall
  • 安装特定 go 版本

    1
    gvm install go1.24.2

初始化 Go 项目

  1. 新建项目文件夹

    1
    2
    mkdir survey-test
    cd survey-test
  2. 初始化 Go 模块

    1
    2
    go mod init github.com/GY23333/survey-test
    go: creating new go.mod: module github.com/GY23333/survey-test

    该步会自动生成 go.mod 文件

    1
    2
    3
    module github.com/GY23333/survey-test

    go 1.23.3
  3. 如需获取依赖

    1
    go get -u github.com/gorilla/mux