Golang程序运行时频繁崩溃问题如何解决

Golang程序运行时频繁崩溃问题如何解决 我最近想重新拾起Go语言来做一些后端服务器工作。于是我又从默认的入门程序开始,但一直遇到一个错误。运行的代码如下:

package main

import "fmt"

func main() {
    fmt.Printf("Hello, world\n")
}

无论我尝试运行哪个Go程序,它总是抛出这个异常并打印以下堆栈信息:

go run hello.go
Exception 0xc0000005 0x0 0x7ffdb0e10fff 0x1ac65b20000
PC=0x1ac65b20000

runtime: unknown pc 0x1ac65b20000
stack: frame={sp:0x799f3fe550, fp:0x0} stack=[0x0,0x799f3ffc30)
000000799f3fe450:  000000799f3fe498  000000799f3fe4c0
000000799f3fe460:  000000799f3fe488  000000799f3fe480
000000799f3fe470:  000000799f3fe484  000001ac5f001140
000000799f3fe480:  0000000000000000  0000000000000000
000000799f3fe490:  0000000000000000  0000000000000005
000000799f3fe4a0:  000000799f3fe5e8  00007ffdae93b3df
000000799f3fe4b0:  000001ac5edc3240  00007ffdb0c4345e
000000799f3fe4c0:  00007ffdb0c100e8  000001ac5ee063c0
000000799f3fe4d0:  00007ffdae93b3d9  000000799f3fe520
000000799f3fe4e0:  004f0044004e0049  0053005c00530057
000000799f3fe4f0:  000001ac5edc3240  0000000000000000
000000799f3fe500:  000001ac5edf3ec0  006l006400050005
000000799f3fe510:  00007ffdae93b3d9  0000000000000000
000000799f3fe520:  00007ffd00000000  00007ffdb0c100e8
000000799f3fe530:  0000000000000000  0000000000000000
000000799f3fe540:  0000000000000001  00007ffdb0c42513
000000799f3fe550: <000001ac00000001  0000000000000000
000000799f3fe560:  0000000000000000  000000799f3fe658
000000799f3fe570:  0000000000000000  0000000000000000
000000799f3fe580:  0000000000000000  0000000000000000
000000799f3fe590:  000001ac5ee063c0  00007ffdb0d61180
000000799f3fe5a0:  000001ac5edc3240  00007ffdb0d637a0
000000799f3fe5b0:  000000000000097d  00007ffdadaeccd8
000000799f3fe5c0:  00007ffdadaea148  00007ffdb0d611a8
000000799f3fe5d0:  00007ffdb0d73fcf  00007ffdadae0000
000000799f3fe5e0:  00007ffdb0d65d94  00007ffdb0c64c60
000000799f3fe5f0:  0000000000000000  0000000000000000
000000799f3fe600:  0000000000000000  0000000000000000
000000799f3fe610:  000001ac5ee076c0  0000000000000040
000000799f3fe620:  0000000000000003  00007ffdb0d7a3f0
000000799f3fe630:  0000000000000001  000000799f3fe900
000000799f3fe640:  000001ac5edf3ec0  00007ffdb0c6eb58
runtime: unknown pc 0x1ac65b20000
stack: frame={sp:0x799f3fe550, fp:0x0} stack=[0x0,0x799f3ffc30)
000000799f3fe450:  000000799f3fe498  000000799f3fe4c0
000000799f3fe460:  000000799f3fe488  000000799f3fe480
000000799f3fe470:  000000799f3fe484  000001ac5f001140
000000799f3fe480:  0000000000000000  0000000000000000
000000799f3fe490:  0000000000000000  0000000000000005
000000799f3fe4a0:  000000799f3fe5e8  00007ffdae93b3df
000000799f3fe4b0:  000001ac5edc3240  00007ffdb0c4345e
000000799f3fe4c0:  00007ffdb0c100e8  000001ac5ee063c0
000000799f3fe4d0:  00007ffdae93b3d9  000000799f3fe520
000000799f3fe4e0:  004f0044004e0049  0053005c00530057
000000799f3fe4f0:  000001ac5edc3240  0000000000000000
000000799f3fe500:  000001ac5edf3ec0  006l006400050005
000000799f3fe510:  00007ffdae93b3d9  0000000000000000
000000799f3fe520:  00007ffd00000000  00007ffdb0c100e8
000000799f3fe530:  0000000000000000  0000000000000000
000000799f3fe540:  0000000000000001  00007ffdb0c42513
000000799f3fe550: <000001ac00000001  0000000000000000
000000799f3fe560:  0000000000000000  000000799f3fe658
000000799f3fe570:  0000000000000000  0000000000000000
000000799f3fe580:  0000000000000000  0000000000000000
000000799f3fe590:  000001ac5ee063c0  00007ffdb0d61180
000000799f3fe5a0:  000001ac5edc3240  00007ffdb0d637a0
000000799f3fe5b0:  000000000000097d  00007ffdadaeccd8
000000799f3fe5c0:  00007ffdadaea148  00007ffdb0d611a8
000000799f3fe5d0:  00007ffdb0d73fcf  00007ffdadae0000
000000799f3fe5e0:  00007ffdb0d65d94  00007ffdb0c64c60
000000799f3fe5f0:  0000000000000000  0000000000000000
000000799f3fe600:  0000000000000000  0000000000000000
000000799f3fe610:  000001ac5ee076c0  0000000000000040
000000799f3fe620:  0000000000000003  00007ffdb0d7a3f0
000000799f3fe630:  0000000000000001  000000799f3fe900
000000799f3fe640:  000001ac5edf3ec0  00007ffdb0c6eb58
rax     0x7ffdadaed87c
rbx     0x7ffdadaed87a
rcx     0x41
rdi     0xffffffffffbadd11
rsi     0x0
rbp     0x7ffdadb1a100
rsp     0x799f3fe550
r8      0x0
r9      0x0
r10     0x0
r11     0x97c
r12     0xc000007a
r13     0x0
r14     0x7ffdadaed87c
r15     0x7ffdb0c10000
rip     0x1ac65b20000
rflags  0x10202
cs      0x33
fs      0x53
gs      0x2b

还有其他人遇到过这个问题吗?或者对于如何解决我遇到的这个问题有什么建议吗?


更多关于Golang程序运行时频繁崩溃问题如何解决的实战教程也可以访问 https://www.itying.com/category-94-b0.html

6 回复

你使用的是哪个操作系统,哪个版本的Go,以及如何安装的Go?

更多关于Golang程序运行时频繁崩溃问题如何解决的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html


你是否使用了任何隐私保护或杀毒软件?或者个人防火墙?

如果使用了,请尝试禁用它们,然后再次尝试在Go中进行操作。

操作系统是 Windows 10。 Go 的版本是 1.16.2。 我是通过从 Go 官网下载的 msi 安装包进行安装的。

作为额外的更新,即使我使用 -buildmode=exe,它也会抛出异常。而且这似乎也给我的网卡带来了问题(所有操作都变慢了)。

我关闭了杀毒软件,问题就消失了。我还在GitHub上开了一个issue,有人指出了这个问题:runtime: Go randomly crashes on Windows with no stack trace, just “unknown pc” and memory/registry dump · Issue #40878 · golang/go (github.com),里面提到Webroot杀毒软件(我用的就是这个)是罪魁祸首。所以我现在得找一个新的杀毒软件了。谢谢你的帮助!

这个错误通常是由于Go工具链损坏或系统环境问题导致的。以下是几种解决方法:

1. 重新安装Go工具链

# 卸载现有Go
sudo rm -rf /usr/local/go

# 下载并安装最新版本
wget https://go.dev/dl/go1.21.0.linux-amd64.tar.gz
sudo tar -C /usr/local -xzf go1.21.0.linux-amd64.tar.gz

2. 清理Go模块缓存

# 清理所有缓存
go clean -cache
go clean -modcache
go clean -testcache

# 或者完全删除缓存目录
rm -rf ~/.cache/go-build
rm -rf ~/go/pkg/mod

3. 检查环境变量配置

# 确保GOROOT正确指向Go安装目录
echo $GOROOT
# 应该输出:/usr/local/go

# 确保PATH包含Go的bin目录
echo $PATH
# 应该包含:/usr/local/go/bin

# 设置正确的环境变量(在~/.bashrc或~/.zshrc中)
export GOROOT=/usr/local/go
export PATH=$GOROOT/bin:$PATH
export GOPATH=$HOME/go
export PATH=$GOPATH/bin:$PATH

4. 使用静态编译排除动态链接问题

// 使用CGO_ENABLED=0进行静态编译
// 编译命令:
// CGO_ENABLED=0 go build -o hello hello.go
package main

import "fmt"

func main() {
    fmt.Printf("Hello, world\n")
}

5. 验证Go安装完整性

# 创建测试程序
cat > test.go << 'EOF'
package main

import (
    "fmt"
    "runtime"
)

func main() {
    fmt.Printf("Go version: %s\n", runtime.Version())
    fmt.Printf("GOOS: %s\n", runtime.GOOS)
    fmt.Printf("GOARCH: %s\n", runtime.GOARCH)
}
EOF

# 编译并运行
go build test.go
./test

6. 检查系统依赖库

# 对于Linux系统,检查glibc版本
ldd --version

# 检查必要的系统库
ldd $(which go)

7. 使用Docker隔离环境测试

# Dockerfile
FROM golang:1.21-alpine
WORKDIR /app
COPY hello.go .
RUN go build -o hello hello.go
CMD ["./hello"]

如果问题仍然存在,可能是系统级别的内存损坏或硬件问题。可以尝试在另一台机器上测试相同的代码,或者使用内存检测工具检查系统稳定性。

回到顶部