本文是针对《Go 语言入门指南:基础语法和常用特性解析》这一选题的实践记录以及工具使用练习,主要内容为:从零开始安装VS Code与GO,运行输出“hello world”。
安装go以及VS Code
一、访问 studygolang.com/dl 下载安装相应的版本
验证是否成功安装(Windows):在终端命令行中输入
go version
命令,若有输出,则为安装成功。以下为个人的尝试以及结果:
C:\Users>go version
go version go1.20.6 windows/amd64
二、在VS Code中安装模块时,出现如下报错。
Tools environment: GOPATH=C:\Users\lnx\go
Installing 7 tools at C:\Users\lnx\go\bin in module mode.
gotests
gomodifytags
goplay
staticcheck
gopls
Installing github.com/cweill/gotests/gotests@v1.6.0 FAILED
"code": 1,
"killed": false,
"signal": null,
"cmd": "D:\\GO\\bin\\go.exe install -v github.com/cweill/gotests/gotests@v1.6.0",
"stdout": "",
"stderr": "go: github.com/cweill/gotests/gotests@v1.6.0: github.com/cweill/gotests/gotests@v1.6.0: Get \"https://proxy.golang.org/github.com/cweill/gotests/gotests/@v/v1.6.0.info\": dial tcp 142.251.43.17:443: connectex: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.\n"
Installing github.com/cweill/gotests/gotests@v1.6.0 FAILED
"code": 1,
"killed": false,
"signal": null,
"cmd": "D:\\GO\\bin\\go.exe install -v github.com/cweill/gotests/gotests@v1.6.0",
"stdout": "",
"stderr": "go: github.com/cweill/gotests/gotests@v1.6.0: github.com/cweill/gotests/gotests@v1.6.0: Get \"https://proxy.golang.org/github.com/cweill/gotests/gotests/@v/v1.6.0.info\": dial tcp 142.251.43.17:443: connectex: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.\n"
以下为搜索到此次报错的相关资料:
安装失败的原因是无法连接到 proxy.golang.org,这可能是由于网络连接问题导致的。可以尝试以下适用于网络连接问题的解决方法:
检查网络连接:确保您的计算机可以访问互联网,并且没有任何防火墙或代理服务器阻止了连接。
更改代理设置:如果您在访问互联网时使用了代理,请检查您的代理设置是否正确,并确保代理服务器正常工作。
使用其他源:将 Go 模块的代理设置更改为其他可用的源,例如 goproxy.cn,可以通过运行以下命令更改:go env -w GOPROXY=https://goproxy.cn,direct
使用 VPN:如果您的网络环境受到限制或屏蔽,请尝试使用 VPN 连接来绕过限制并重新运行安装命令。
结合网课教程进行分析,运行go env -w GOPROXY=https://goproxy.cn,direct
设置go包代理解决问题。成功安装模块的提示如下。
Tools environment: GOPATH=C:\Users\lnx\go
Installing 1 tool at C:\Users\lnx\go\bin in module mode.
gopls
Installing github.com/ramya-rao-a/go-outline@v0.0.0-20210608161538-9736a4bde949 (C:\Users\lnx\go\bin\go-outline.exe) SUCCEEDED
All tools successfully installed. You are ready to Go. :)
三、设置go包校验以及查看go环境
C:\Users\lnx>go env -w GOSUMDB=sum.golang.google.cn
C:\Users\lnx>go env
set GO111MODULE=
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\lnx\AppData\Local\go-build
set GOENV=C:\Users\lnx\AppData\Roaming\go\env
set GOEXE=.exe
set GOEXPERIMENT=
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GOMODCACHE=C:\Users\lnx\go\pkg\mod
set GONOPROXY=
set GONOSUMDB=
set GOOS=windows
set GOPATH=C:\Users\lnx\go 【改换默认安装位置后未设置环境变量,导致仍将工具等安装在默认位置】
set GOPRIVATE=
set GOPROXY=https://goproxy.cn,direct
set GOROOT=D:\GO
set GOSUMDB=sum.golang.google.cn
set GOTMPDIR=
set GOTOOLDIR=D:\GO\pkg\tool\windows_amd64
set GOVCS=
set GOVERSION=go1.20.6
set GCCGO=gccgo
set GOAMD64=v1
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=0
set GOMOD=NUL
set GOWORK=
set CGO_CFLAGS=-O2 -g
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-O2 -g
set CGO_FFLAGS=-O2 -g
set CGO_LDFLAGS=-O2 -g
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -fno-caret-diagnostics -Qunused-arguments -Wl,--no-gc-sections -fmessage-length=0 -fdebug-prefix-map=C:\Users\lnx\AppData\Local\Temp\go-build1278392580=/tmp/go-build -gno-record-gcc-switches
运行文件(hello world)
法一:在侧边栏运行与调试中打开文件,并运行。在调试控制台中有如下输出。
Starting: C:\Users\lnx\go\bin\dlv.exe dap --listen=127.0.0.1:52104 from c:\Users\lnx\Desktop\Bytedance\go-by-example-master\example\01-hello
DAP server listening at: 127.0.0.1:52104
hello world
Process 2348 has exited with status 0
法二:在VS Code中添加code runner扩展,右击文件即可运行。
[Running] go run "c:\Users\lnx\Desktop\Bytedance\go-by-example-master\example\01-hello\main.go"
hello world
[Done] exited with code=0 in 0.996 seconds
法三:命令行中执行(速度较慢)如下。
PS C:\Users\lnx\Desktop\Bytedance\go-by-example-master> go run example/01-hello/main.go
hello world
个人感想:网课给出的教程过于简略,只有一定的参考价值。需要在其它平台上寻找更为详细的教程结合相看。操作步骤不能漏,也不能在不懂含义的情况下就擅自调顺序。否则之后更改更麻烦,且会遇到各种各样奇奇怪怪的报错:(
lnx981