在树莓派4上安装Golang的详细教程

在树莓派4上安装Golang的详细教程 我之前从未使用过树莓派,但出于教育目的正在尝试。我使用的是树莓派 4 型号 B。我正在尝试按照这个视频教程安装 Go。

我遇到了一个问题;运行代码后,我收到以下错误信息:

raspberry@raspberrypi:~$ go install $GOPATH/hello.go
# internal/goarch
compile: version "go1.21.0" does not match go tool version "go1.19.8"
# internal/unsafeheader
compile: version "go1.21.0" does not match go tool version "go1.19.8"
# internal/byteorder
compile: version "go1.21.0" does not match go tool version "go1.19.8"
# internal/coverage/rtcov
compile: version "go1.21.0" does not match go tool version "go1.19.8"
# internal/godebugs
compile: version "go1.21.0" does not match go tool version "go1.19.8"
# internal/cpu
compile: version "go1.21.0" does not match go tool version "go1.19.8"
# internal/goexperiment
compile: version "go1.21.0" does not match go tool version "go1.19.8"
# internal/goos
compile: version "go1.21.0" does not match go tool version "go1.19.8"
# internal/profilerecord
compile: version "go1.21.0" does not match go tool version "go1.19.8"
# internal/itoa
compile: version "go1.21.0" does not match go tool version "go1.19.8"
# cmp
compile: version "go1.21.0" does not match go tool version "go1.19.8"
# internal/runtime/syscall
compile: version "go1.21.0" does not match go tool version "go1.19.8"
# internal/race
compile: version "go1.21.0" does not match go tool version "go1.19.8"
# math/bits
compile: version "go1.21.0" does not match go tool version "go1.19.8"
# unicode
compile: version "go1.21.0" does not match go tool version "go1.19.8"
# unicode/utf8
compile: version "go1.21.0" does not match go tool version "go1.19.8"
# sync/atomic
compile: version "go1.21.0" does not match go tool version "go1.19.8"
# internal/asan
compile: version "go1.21.0" does not match go tool version "go1.19.8"
# internal/msan
compile: version "go1.21.0" does not match go tool version "go1.19.8"

在我看来,我安装的 Go 版本不正确(是 1.21.0 而不是 1.19.8)。不过我记得我安装的是 1.23.2 版本。我毫无经验,不知道如何修复或哪里出了问题。有人知道如何解决这个问题,或者知道更好的教程吗?我尝试过的其他教程也都没有成功。


更多关于在树莓派4上安装Golang的详细教程的实战教程也可以访问 https://www.itying.com/category-94-b0.html

6 回复

然后我收到消息说它没有被找到。

更多关于在树莓派4上安装Golang的详细教程的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html


尝试执行 cat $GOPATH/hello.go

尝试执行 go version 命令? 查看 https://go.dev/

我试过了,它显示:

warning: GOPATH set to GOROOT (/home/raspberry/.local/share/go) has no effect
go version go1.22.1 linux/arm64

MadeleineWijnberg:

raspberry[@raspberrypi](/user/raspberrypi):~$ go install $GOPATH/hello.go

你正在运行这个命令,为什么不能检查一下 $GOPATH/hello.go 呢?

这个错误表明你的系统中存在多个Go版本,并且版本不匹配。go1.21.0 是编译时要求的版本,但当前激活的 go toolgo1.19.8。以下是解决步骤:

首先,彻底移除所有现有的Go安装:

# 删除通过apt安装的Go
sudo apt remove --purge golang golang-go
sudo apt autoremove

# 删除手动安装的Go(通常在/usr/local/go)
sudo rm -rf /usr/local/go

# 清理环境变量
# 编辑 ~/.bashrc 或 ~/.profile,移除所有GOROOT、GOPATH相关设置
nano ~/.bashrc

然后,从官方源安装最新版Go(当前是1.23.2):

# 下载ARMv6l版本(树莓派4适用)
wget https://go.dev/dl/go1.23.2.linux-armv6l.tar.gz

# 删除旧版本并解压新版本
sudo rm -rf /usr/local/go
sudo tar -C /usr/local -xzf go1.23.2.linux-armv6l.tar.gz

# 设置环境变量
echo 'export PATH=$PATH:/usr/local/go/bin' >> ~/.bashrc
echo 'export GOPATH=$HOME/go' >> ~/.bashrc
source ~/.bashrc

验证安装:

go version
# 应该显示: go version go1.23.2 linux/arm

创建测试程序:

mkdir -p ~/go/src/hello
cd ~/go/src/hello
nano hello.go
package main

import "fmt"

func main() {
    fmt.Println("Hello, Raspberry Pi!")
}

编译运行:

go run hello.go
# 输出: Hello, Raspberry Pi!

如果仍有版本冲突,检查是否有残留:

which -a go
# 应该只显示: /usr/local/go/bin/go

go env GOROOT
# 应该显示: /usr/local/go

对于树莓派4,确保下载正确的架构版本。如果使用64位系统,使用linux-arm64版本:

wget https://go.dev/dl/go1.23.2.linux-arm64.tar.gz
回到顶部