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
?
CodePudding user response:
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 whell.
OK, we've almost arrived at the conclusion ;-)
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 form 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.