Go 常见问题
Go常见问题
问题: go.mod file not found
现象
go.mod file not found in current directory or any parent directory.
原因
Go1.16之后引入GoMod, 不需要基于GoPath进行包管理
在创建新项目的时候需要初始化模块
解决
# 初始化模块
go mod init example/hello
问题: GoLand无法选择Go版本
现象
The selected directory is not a valid home for Go SDK
原因
Goland是通过src内的源码文件读取版本信息的.
解决
打开文件:
${GOROOT}/src/runtime/internal/sys/zversion.go
添加版本号如下:
const TheVersion = `go1.20`
命令行:
echo -e "\nconst TheVersion = \`$(cat ${GOROOT}/VERSION)\`" >> ${GOROOT}/src/runtime/internal/sys/zversion.go
问题: Goland不显示代码提示
现象
原因
加载的GoRoot的GO环境中, 没有设置代理
解决
IDE -> File -> Settings -> Go -> GoModules
勾选: Enable Go modules integration
GOPROXY=https://goproxy.io
即可.
问题: Go项目编译时随机崩溃, 0xc0000005 0x0 0x1e1c5a53150 0x7ffce8a8ce20
现象
IDE或CMD编译Go项目的时候, 会有一定概率随机出现内存错误.
日志如下:
Exception 0xc0000005 0x0 0x1e1c5a53150 0x7ffce8a8ce20
PC=0x7ffce8a8ce20
runtime.cgocall(0x448be0, 0xc000bc26c0)
c:/go/src/runtime/cgocall.go:157 +0x4a fp=0xc000c48678 sp=0xc000c48640 pc=0x3e4d2a
syscall.SyscallN(0xc000c48838?, {0xc000c48710?, 0xc001184800?, 0xc00112d900?})
c:/go/src/runtime/syscall_windows.go:557 +0x109 fp=0xc000c486f0 sp=0xc000c48678 pc=0x443de9
syscall.Syscall12(0x0?, 0x0?, 0x0?, 0xc000c487e8?, 0x42b7f2?, 0xe3b9c0?, 0xc000c48818?, 0xc0003f8f00?, 0xc000c48838?, 0x0, ...)
c:/go/src/runtime/syscall_windows.go:513 +0xac fp=0xc000c48780 sp=0xc000c486f0 pc=0x443ccc
syscall.CreateProcess(0x57?, 0xc000c488a0?, 0x3ed7cd?, 0x10100000000?, 0x1, 0x80400, 0x103d3f8?, 0xc000c488c0?, 0x401d85?, 0xc000c48908)
c:/go/src/syscall/zsyscall_windows.go:545 +0x114 fp=0xc000c48848 sp=0xc000c48780 pc=0x45eab4
syscall.StartProcess({0xc0007705a0?, 0x6cafbe6500000400?}, {0xc00035ce00, 0x2, 0x2}, 0xc0001ae3a8?)
c:/go/src/syscall/exec_windows.go:418 +0xc1a fp=0xc000c48aa8 sp=0xc000c48848 pc=0x45927a
os.startProcess({0xc0007705a0, 0x2c}, {0xc00035ce00, 0x2, 0x2}, 0xc000c48ce8)
c:/go/src/os/exec_posix.go:54 +0x311 fp=0xc000c48b88 sp=0xc000c48aa8 pc=0x4ce591
os.StartProcess({0xc0007705a0, 0x2c}, {0xc00035ce00, 0x2, 0x2}, 0x3ed81e?)
c:/go/src/os/exec.go:109 +0x5a fp=0xc000c48bd0 sp=0xc000c48b88 pc=0x4cdfda
os/exec.(*Cmd).Start(0xc00094a160)
c:/go/src/os/exec/exec.go:693 +0x62e fp=0xc000c48d48 sp=0xc000c48bd0 pc=0x5047ce
os/exec.(*Cmd).Run(0xc0007705a0?)
c:/go/src/os/exec/exec.go:587 +0x1e fp=0xc000c48d68 sp=0xc000c48d48 pc=0x503efe
cmd/go/internal/work.(*Builder).toolID(0xc000150370, {0xa24e28, 0x3})
c:/go/src/cmd/go/internal/work/buildid.go:167 +0x351 fp=0xc000c48e80 sp=0xc000c48d68 pc=0x8b3c11
cmd/go/internal/work.(*Builder).buildActionID(0xc000150370, 0xc0007fe580)
c:/go/src/cmd/go/internal/work/exec.go:323 +0xf94 fp=0xc000c49248 sp=0xc000c48e80 pc=0x8ba154
cmd/go/internal/work.(*Builder).build(0xc000150370, {0xc000770444?, 0x0?}, 0xc0007fe580)
c:/go/src/cmd/go/internal/work/exec.go:467 +0x199 fp=0xc000c49d60 sp=0xc000c49248 pc=0x8bb379
cmd/go/internal/work.ActorFunc.Act(0xb273e8?, 0xc00001c060?, {0xb273e8?, 0xc00001c060?}, 0x4283d3?)
c:/go/src/cmd/go/internal/work/action.go:76 +0x33 fp=0xc000c49d90 sp=0xc000c49d60 pc=0x8aa213
cmd/go/internal/work.(*Builder).Do.func2({0xb273e8, 0xc00001c060}, 0xc0007fe580)
c:/go/src/cmd/go/internal/work/exec.go:143 +0x7a4 fp=0xc000c49f20 sp=0xc000c49d90 pc=0x8b8ea4
cmd/go/internal/work.(*Builder).Do.func3()
c:/go/src/cmd/go/internal/work/exec.go:211 +0xb9 fp=0xc000c49fe0 sp=0xc000c49f20 pc=0x8b8519
runtime.goexit()
c:/go/src/runtime/asm_amd64.s:1598 +0x1 fp=0xc000c49fe8 sp=0xc000c49fe0 pc=0x447261
created by cmd/go/internal/work.(*Builder).Do
c:/go/src/cmd/go/internal/work/exec.go:197 +0x3f5
goroutine 1 [semacquire]:
runtime.gopark(0xc0003c9960?, 0x20?, 0xa0?, 0xb?, 0x9c79a0?)
c:/go/src/runtime/proc.go:381 +0xd6 fp=0xc00119ba38 sp=0xc00119ba18 pc=0x41a776
runtime.goparkunlock(...)
c:/go/src/runtime/proc.go:387
runtime.semacquire1(0xc0008aff18, 0x30?, 0x1, 0x0, 0xb1?)
c:/go/src/runtime/sema.go:160 +0x21b fp=0xc00119baa0 sp=0xc00119ba38 pc=0x429ddb
sync.runtime_Semacquire(0xc000b45d10?)
c:/go/src/runtime/sema.go:62 +0x27 fp=0xc00119bad8 sp=0xc00119baa0 pc=0x4424e7
sync.(*WaitGroup).Wait(0x0?)
c:/go/src/sync/waitgroup.go:116 +0x4b fp=0xc00119bb00 sp=0xc00119bad8 pc=0x455b8b
cmd/go/internal/work.(*Builder).Do(0xc000150370, {0xb273e8, 0xc00001c060}, 0xc00058ab00)
c:/go/src/cmd/go/internal/work/exec.go:220 +0x4ca fp=0xc00119bc18 sp=0xc00119bb00 pc=0x8b82aa
cmd/go/internal/work.runBuild({0xb273e8, 0xc00001c060}, 0xc000028ff0?, {0xc000120040, 0x1, 0x4})
c:/go/src/cmd/go/internal/work/build.go:534 +0x7f2 fp=0xc00119bd80 sp=0xc00119bc18 pc=0x8b12b2
main.invoke(0xe438a0, {0xc000120010, 0x4, 0x7})
c:/go/src/cmd/go/main.go:225 +0x3d9 fp=0xc00119be58 sp=0xc00119bd80 pc=0x93b6f9
main.main()
原因
该问题为因Windows系统环境异常从而引起, 导致随机崩溃的问题.
使用最简单的HelloWorld代码测试, 如下,
package main
func main() {
print("Hello,World")
}
也同样会引起该类似的错误, 唯一的区别是该随机触发的概率有所降低.(?代码量越多, 则越容易触发)
推断可能是因为安全软件引起的环境问题.
- 可能影响的防火墙类/ 安全卫士类的 软件:
- Webroot SecureAnywhere
- XXX安全助手: ×, 卸载后再未出现过类似问题
解决
排查对应的安全助手或防火墙软件, 卸载即可.