为什么我必须使用~/.bash_profile来使用Go?

huangapple go评论89阅读模式
英文:

why i have to use ~/.bash_profile to use Go?

问题

你好!根据你的描述,你在Ubuntu上安装了Go,并且只有在运行source ~/.bash_profile之后才能使用go命令。如果你希望无需每次都运行source ~/.bash_profile就能使用Go命令,你可以将以下内容添加到你的.bashrc文件中:

export PATH=$PATH:/usr/local/go/bin

保存文件后,重新打开终端或者运行source ~/.bashrc命令,你应该就能够直接使用go命令了。这样设置后,系统会将Go的可执行文件路径添加到环境变量中,使得你可以在任何目录下直接运行go命令。

希望这对你有帮助!如果你还有其他问题,请随时提问。

英文:

I used to use Go on windows. And i recently use ubuntu,and i'm newbie in linux environtment. i install using binary to install Go with this tutorial (method #3). But when i type go command like go version, the terminal said that 'command not found'.I can use the go command only after i use source ~/.bash_profile

how can i use Go, without initializing source ~/.bash_profile ?

答案1

得分: 1

有多个事情正在进行,其中没有一个与Go直接相关。

一些理论

我假设你一开始就在某个“图形桌面环境”中工作;也就是说,当你的计算机启动时,你会看到一个图形登录对话框,然后你会有一个“桌面”,所有的应用程序都在单独的窗口中运行。

其中一个窗口是“终端”或“控制台”。
在你典型的Ubuntu系统上,你按下Windows键,然后键入“Terminal”来打开它。
实际上,它并不是真正启动了一个终端,而是一个名副其实的终端仿真器,它运行一个命令行shell,在你的情况下是bash
Shell会呈现给你命令行,接受你的输入,运行命令,而终端仿真器通过“托管”Shell使所有这些工作正常运行。

~/.bash_profile文件是bash在启动时根据某些条件读取的初始化文件之一——稍后会详细介绍。

在基于Linux的操作系统(实际上是在任何类Unix操作系统上,以及Mac OS和Windows上),所有运行的进程都有环境变量的概念。环境变量有一个名称和一个值。你在指南的第4步中调整的PATH环境变量包含了计算机上用于查找与你在Shell中输入的命令的短名称匹配的文件的目录列表。
因此,当你输入go version时,你要求你的Shell查找并运行一个名为go的程序,并将一个参数version传递给它。由于你没有拼写该命令的完整路径名,Shell必须在一组众所周知的位置中搜索它,而PATH包含了这个集合。

环境变量的一个特性是它们是继承的:如果一个进程启动另一个进程,那个新进程将继承其父进程的环境变量。
这允许在你的“登录会话”早期设置某些环境变量——当你成功验证系统时创建的会话。而PATH环境变量就是其中之一,它在早期设置并包含了操作系统提供的所有“默认”程序安装的一组目录。

当你使用那个指南安装Go时,它并没有安装在任何这些默认的众所周知的位置,所以当你要求Shell运行go时,它无法找到它。

命令行Shell的一个特性是它们允许轻松地操作环境变量——包括在它们的启动脚本中设置它们。
你指南中的代码段
export PATH=$PATH:/usr/local/go/bin:$GOPATH/bin
就是这样做的:修改PATH环境变量,然后“导出”它的新值,这意味着使它对Shell运行的进程可用。

好了,我们几乎要得出结论了 为什么我必须使用~/.bash_profile来使用Go?

当你在终端仿真器中运行一个Shell时,从Shell的角度来看,它被称为交互非登录模式——它是交互式的,因为用户(你)将直接在其中键入命令,它是非登录的,因为它不是由管理用户登录的进程启动的。
当以这种模式运行bash Shell时,它会读取文件~/.bashrc——详细说明在它的手册中,只有在作为登录Shell运行时才会读取~/.bash_profile,而这不是你的情况。

TL;DR

将以下内容添加到你的~/.bashrc而不是~/.bash_profile,然后再打开一个终端窗口——它将立即生效。
这是正确的做法。

另一种方法是从你解压Go的位置中删除它,并安装你的操作系统提供的golang-go软件包。它将以一种无需干扰环境变量的方式安装。

讨论从官方网站安装还是从操作系统安装的相对优点与问题与本问题无关,所以我不会赘述。


如果你打算在类Unix环境中认真开发软件,建议你找一本关于Unix作为工作环境的书来阅读。

...顺便说一下,这种语言被称为Go

英文:

There are multiple things going on, and none of them is directly related to Go.

Some theory

I assume you're working in some "graphical desktop environment" to begin with; that is, when your computer boots up, you're presented with a graphical login dialog and then you have that "desktop" and all applications run in separate windows.

One such window is "a terminal" or "a console".
On your typical Ubuntu system you hit the Windows key and then type "Terminal" to open it.
In reality, it's not really a terminal which is started, but rather an aptly named terminal emulator which runs a command-line shell, in your case — bash.
The shell presents you with the command line, accepts your input, runs commands, and the temrminal emulator makes it all work by "hosting" the shell.

That ~/.bash_profile file is one of the initialization files read by bash on startup under certain conditions — more on that in a moment.

On Linux-based operating systems (actually on any Unix-linke operating system, as well as on Mac OS and Windows) all running processes have the concept of environment variables. An environment variable has a name and a value. That PATH environment variable you have tweaked on step 4 of that "method 3" in your guide contains a list of directories on your computer to look up for files matching short name of the command you type in your shell.
So, when you type go version, you ask your shell to look up and run a program named go and pass it a single argument version. Since you do not spell the full pathname of that command, the shell has to search it in a set of well-known places, and PATH contains that set.

A property of environment variables is that they are inherited: if a process starts another process, that new one inherits the environment variables of its parent.
This allows setting certain environment variables very early in your login session ­— which is created when you have successfully authenticated to the system. And the PATH environment variable is one of such things which is set early and contains a set of directories into which all of the "stock" programs provided by the OS are installed.

When you have installed Go using that guide, it was not installed in any of those default well-known locations, so shells cannot find it when you ask them to run go.

A property of command-line shells is that they allow easy manipulating of environment variables — including setting them in their startup scripts.
The snippet
export PATH=$PATH:/usr/local/go/bin:$GOPATH/bin
from your guide does just that: modifies the PATH environment variable and then "exports" its new value which means making it available for the processes run by the shell.

OK, we've almost arrived at the conclusion 为什么我必须使用~/.bash_profile来使用Go?

When you run a shell in a terminal emulator, from the point of view of the shell it's called an interactive non-login mode — it's interactive because the user, you, is about to directly type commands into it, and it's non-login because it was not started by a process managing users' logins.
The bash shell, when run in this mode, reads the file ~/.bashrc — as detailed in its manual, and it only reads ~/.bash_profile when run as a login shell, which is not your case.

TL;DR

Stick

export GOPATH=$HOME/go
export PATH=$PATH:/usr/local/go/bin:$GOPATH/bin

to your ~/.bashrc instead of ~/.bash_profile and spawn another terminal window—it will work instantly.
That's the correct way of doing that.

Another way is to just delete Go from where you have unpacked it and install the golang-go package provided by your OS. It will be installed in a way you will not need to mess with environment variables.

Discussing relative merits of installing from the official site or from the OS is tangential to the problem, so I will not digress.


It's advised to fetch a book on Unix as a working environment and read it, if you intend to get serious about developing software in a Unix-like environment.

…And while we're at it, the language is called Go.

huangapple
  • 本文由 发表于 2023年1月3日 23:31:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/74995391.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定