简体   繁体   English

在 Raspbian 上安装 go (golang)

[英]Install go (golang) on Raspbian

I checked diverse forums but I still did not make it working.我检查了各种论坛,但仍然无法正常工作。 I like to install go (golang) on my Raspberry PI - Raspbian:我喜欢在我的 Raspberry PI - Raspbian 上安装 go (golang):

With

sudo apt-get install golang

I installed go and with我安装了 go and with

export GOPATH=$home/pi/gocode

i set the GOPATH so i tryed to install from a homepage a new program with ( sudo go get -u github.com/.... ) but, I only get " cannot download, $GOPATH not set. For more details see: go help gopath ".我设置了 GOPATH,所以我尝试从主页安装一个新程序( sudo go get -u github.com/.... )但是,我只得到“ cannot download, $GOPATH not set. For more details see: go help gopath ”。

I really get crazy for my studip simple mistake that i do not see.我真的为我没有看到的学习简单错误而疯狂。

I would be pleased if i get a very detailed "how to do" discription since I am new to Linux and Raspbian, so everything which is made for real dummys should be good enough for me.如果我得到一个非常详细的“如何做”的描述我会很高兴,因为我是 Linux 和 Raspbian 的新手,所以所有为真正的傻瓜制作的东西对我来说应该足够了。 Thank you for your help.谢谢你的帮助。

This are detailed instructions about how to install Go on Raspbian Stretch from the repositories.这是有关如何从存储库安装 Go on Raspbian Stretch 的详细说明。

As of today, 2018-01-30, this will install Go 1.7.截至今天,2018-01-30,这将安装 Go 1.7。 The most actual version for manual installation from the downloads is Go 1.9.3.从下载中手动安装的最实际版本是 Go 1.9.3。

I. Login to your user on the Raspberry Pi (I'm using the default user pi). I. 在 Raspberry Pi 上登录您的用户(我使用的是默认用户 pi)。

II.二、 Install Go (golang)安装 Go (golang)

pi@pi3-2:~ $ sudo apt update 
pi@pi3-2:~ $ sudo apt install golang

III.三、 Create a working directory for all your go projects in your $HOME directory.在 $HOME 目录中为所有 go 项目创建一个工作目录。 It's best to name it go, as this defaults to the GOPATH in future Go versions (starting with Go 1.8).最好将其命名为 go,因为在未来的 Go 版本(从 Go 1.8 开始)中默认为 GOPATH。

pi@pi3-2:~ $ mkdir go

IV.四、 Append the $GOPATH environment variable and modified PATH settings to your .profile将 $GOPATH 环境变量和修改后的 PATH 设置附加到您的 .profile

pi@pi3-2:~ $ echo 'export GOPATH=$HOME/go' >> ~/.profile
pi@pi3-2:~ $ echo 'PATH="$HOME/go/bin:$PATH"' >> ~/.profile

V. Logout and Relog with the new settings then check your settings V. 注销并使用新设置重新登录,然后检查您的设置

pi@pi3-2:~ $ go env

GOARCH="arm"
GOBIN=""
GOEXE=""
GOHOSTARCH="arm"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/pi/go"
GORACE=""
GOROOT="/usr/lib/go-1.7"
GOTOOLDIR="/usr/lib/go-1.7/pkg/tool/linux_arm"
CC="gcc"
GOGCCFLAGS="-fPIC -marm -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build187598155=/tmp/go-build -gno-record-gcc-switches"
CXX="g++"
CGO_ENABLED="1"

Especially make sure GOPATH points to your previously created Go working directory.尤其要确保 GOPATH 指向您之前创建的 Go 工作目录。 Don't care about setting GOBIN as mentioned in some documentation.不关心在某些文档中提到的设置 GOBIN。 It's usually not necessary and Go will automatically use $GOPATH/bin/ for your Go installs.通常没有必要,Go 会自动使用 $GOPATH/bin/ 进行 Go 安装。

However, you might also want to check the path settings (/home/pi/go/bin should be included) to make sure you can run the code you installed with go install.但是,您可能还想检查路径设置(应包含 /home/pi/go/bin)以确保您可以运行使用 go install 安装的代码。

pi@pi3-2:~ $ echo $PATH
/home/pi/go/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/games:/usr/games

VI.六、 A few words about the Go working directory structure关于 Go 工作目录结构的几句话

Over time, the Go working directory will contain three sub-directories: bin, src and pkg.随着时间的推移,Go 工作目录将包含三个子目录:bin、src 和 pkg。 Except src they will be automatically created, when needed the first time.除了 src 它们将在第一次需要时自动创建。 The structure for user pi will look like this:用户 pi 的结构如下所示:

/home
  /pi
    /go
      /src
      /pkg
      /bin

bin will contain all Go executable's you have installed using go install command. bin将包含您使用 go install 命令安装的所有 Go 可执行文件。

pkg will contain all compiled packages that can be imported into your projects. pkg将包含所有可以导入到您的项目中的编译包。

src will contain all your source files, either your own or sources downloaded from external repositories. src将包含您所有的源文件,无论是您自己的还是从外部存储库下载的源文件。

For eksample the command go get github.com/petergloor/hello-go will automatically fetch and place the source files from the corresponding external Github repository into the local directory $HOME/go/src/github.com/petergloor/hello-go .对于 eksample 命令go get github.com/petergloor/hello-go将自动从相应的外部 Github 存储库中获取源文件并将其放入本地目录$HOME/go/src/github.com/petergloor/hello-go

As it's rather common to fetch external repositories either for reference or contribution it becomes important to keep your directory structure always well organized and clean.由于获取外部存储库以供参考或贡献是相当普遍的,因此保持目录结构始终井井有条和干净就变得很重要。

Apart from that you are free to organize your projects as long they are hierarchically structured below the $HOME/go/src/ directory and follow the rules mentioned in the documentation.除此之外,您可以自由组织您的项目,只要它们在 $HOME/go/src/ 目录下分层结构并遵循文档中提到的规则。

However, to clearly organize my projects I personally always place my projects into $HOME/go/src/github.com/my-github-account even if I don't have an external repository for it.然而,为了清楚地组织我的项目,我个人总是将我的项目放在 $HOME/go/src/github.com/my-github-account 中,即使我没有外部存储库。

If you don't have a github account you can likewise use any other external repository account.如果您没有 github 帐户,您也可以使用任何其他外部存储库帐户。

As I mentioned, even it's not needed at all I prefere to use my Github account to clearly identify my projects.正如我所提到的,即使根本不需要我也更喜欢使用我的 Github 帐户来清楚地识别我的项目。 And even it's not needed I will use the username pi to distinct the user from other project maintainers in the following example.即使不需要,我将在以下示例中使用用户名 pi 将用户与其他项目维护者区分开来。

VII.七、 So let's add a "hello world" project to test our installation.所以让我们添加一个“hello world”项目来测试我们的安装。

a) First let's create the project folder and cd into its directory. a) 首先让我们创建项目文件夹并将 cd 放入其目录。

pi@pi3-2:~ $ mkdir -p $HOME/go/src/pi/helloworld
pi@pi3-2:~ $ cd $HOME/go/src/pi/helloworld
pi@pi3-2:~/go/src/pi/helloworld $

b) With an editor of your choice create a file main.go with the following content b) 使用您选择的编辑器创建一个包含以下内容的文件 main.go

// helloworld project main.go.
package main

import ("fmt")

// main is the entrypoint of the application.
func main() {
  fmt.Println("Hello world! Greetings from Raspberry Pi")
}

Spacing doesn't matter at this point.间距在这一点上并不重要。 Go provides a nice tool to do this for you. Go 提供了一个很好的工具来为你做这件事。

c) Now try to run the program. c) 现在尝试运行该程序。

pi@pi3-2:~/go/src/pi/helloworld $ go run main.go
Hello world! Greetings from Raspberry Pi
pi@pi3-2:~/go/src/pi/helloworld $

In case you get an error, fix it!如果出现错误,请修复它! Carefully, check the spelling and cases (Go is case-sensitive).仔细检查拼写和大小写(Go 区分大小写)。

d) Next lets format the code: d) 接下来让我们格式化代码:

pi@pi3-2:~/go/src/pi/helloworld $ go fmt

Without a file name this will properly (re-)format all source files within this directory and below.如果没有文件名,这将正确(重新)格式化此目录及以下目录中的所有源文件。

e) Next let's build helloworld as an executable procram, within this directory. e) 接下来让我们在此目录中将 helloworld 构建为可执行程序。

pi@pi3-2:~/go/src/pi/helloworld $ go build
pi@pi3-2:~/go/src/pi/helloworld $ ls
helloworld  main.go
pi@pi3-2:~/go/src/pi/helloworld $

f) Now you can run it. f) 现在您可以运行它了。

pi@pi3-2:~/go/src/pi/helloworld $ ./helloworld
Hello world! Greetings from Raspberry Pi
pi@pi3-2:~/go/src/pi/helloworld $

g) Finally let's install the program into the $HOME/go/bin/ directory. g) 最后让我们将程序安装到 $HOME/go/bin/ 目录中。

pi@pi3-2:~/go/src/pi/helloworld $ go install
pi@pi3-2:~/go/src/pi/helloworld $ ls $HOME/go/bin
hello-go  helloworld
pi@pi3-2:~/go/src/pi/helloworld $

h) If everything is done right it can be run by our pi user from anywhere by just entering the name of the command. h) 如果一切顺利,我们的 pi 用户只需输入命令的名称即可从任何地方运行它。

pi@pi3-2:~/go/src/pi/helloworld $ helloworld
Hello world! Greetings from Raspberry Pi
pi@pi3-2:~/go/src/pi/helloworld $ cd ~
pi@pi3-2:~ $ helloworld
Hello world! Greetings from Raspberry Pi
pi@pi3-2:~ $

Congratulations!恭喜!

as of Nov 2019. (Please note if you need version 1.13+ please wget go1.13.3.linux-amd64.tar.gz manually) OR sudo apt-get install golang-go - source截至 2019 年 11 月。(请注意,如果您需要 1.13+ 版本,请手动 wget go1.13.3.linux-amd64.tar.gz )或sudo apt-get install golang-go - source

You just have to type following commands on your RPi你只需要在你的 RPi 上输入以下命令

sudo apt-get update
sudo apt-get install golang --fix-missing

and when you type now go version following should be your output当你现在输入go version以下应该是你的输出

pi@rpi1:~ $ go version
go version go1.11.6 linux/arm

Additionally, I also had the following installed before running everything sudo apt-get install make gcc g++此外,在运行所有内容之前,我还安装了以下内容sudo apt-get install make gcc g++

在此处输入图片说明

How to install golang: golang的安装方法:

Download the latest tarball, go1.9.linux-armv6l.tar.gz , to a directory such as /home/pi/downloads .将最新的 tarball go1.9.linux-armv6l.tar.gz下载到/home/pi/downloads 等目录

Then use:然后使用:

sudo tar -C /home/pi -xzf go1.9.linux-armv6l.tar.gz

to extract the go installation to the directory将 go 安装解压到目录

/home/pi /家/pi

creating /home/pi/go创建/home/pi/go

to check use检查使用

go version

Which will give you the actual version of go.这将为您提供实际版本的 go。

It should be go1.9 linux/arm.应该是 go1.9 linux/arm。

Please check with:请检查:

go env

or

go env GOPATH

the GOPATH direction GOPATH方向

with

ls -a # (used in /home/pi/ )

give you a list of all files and also shows you ~/.profile给你一个所有文件的列表,并显示你~/.profile

with

sudo nano ~/.profile

you can open that file an add the reccommended code for the go directory您可以打开该文件并为 go 目录添加推荐的代码

 export GOROOT=/home/pi/go
 export GOPATH=/home/pi/go/bin

close with STRG + O and ENTER and STRG + X用 STRG + O 和 ENTER 和 STRG + X 关闭

check with检查

go env GOPATH

then use然后使用

source ~/.profile

then you can check again with然后你可以再次检查

go env

Now go should be running on the latest version and should have the correct GOPATH directory现在 go 应该在最新版本上运行并且应该有正确的GOPATH目录

For me helpful was this link: https://tecadmin.net/install-go-on-debian/#这个链接对我很有帮助: https : //tecadmin.net/install-go-on-debian/#

I really hope some others also give detailed descriptions of how to install a program in a brief and detailed way.我真的希望其他人也能以简短而详细的方式详细说明如何安装程序。 Instead of 3 lines of single code.而不是 3 行单一代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM