i installed go for ubuntu as explained here https://golang.org/doc/install and here https://tour.golang.org/welcome/3 ("When you run the tour program, it will open a web browser displaying your local version of the tour" but it does not)
works expected:
tar -C /usr/local -xzf go1.15.3.linux-amd64.tar.gz
export PATH=$PATH:/usr/local/go/bin
BTW i needed to usa a sudo prefix: sudo tar -C /usr/local -xzf go1.15.3.linux-amd64.tar.gz
that looks like expected:
go version
go version go1.13.8 linux/amd64
go get golang.org/x/tour
no go files listed (not expected):
then i tried go run tour.go
to open the local version of the tour in a web browser
and got this errors:
$ go run tour.go
go: downloading golang.org/x/tools v0.0.0-20190312164927-7b79afddac43
go: extracting golang.org/x/tools v0.0.0-20190312164927-7b79afddac43
go: finding golang.org/x/tools v0.0.0-20190312164927-7b79afddac43
# command-line-arguments
./tour.go:63:4: undefined: analyticsHTML
./tour.go:63:19: undefined: socketAddr
./tour.go:127:28: undefined: prepContent
m#kubuntu-MS-7C37:~/go/src/golang.org/x/tour$ go run
go run: no go files listed
go tool: no such tool "tour"
then i tired $ go tool tour got error go tool: no such tool "tour" ( https://github.com/golang/go/issues/24819#issuecomment-380777527 )
`
My Working Environment is Kubuntu 20.04.
After go get golang.org/x/tour, your tour binary will located at ~/go/bin.(here ~ means GO_HOME, default is your home folder)
then you can echo "export PATH=\$PATH:~/go/bin" >> ~/.bashrc && source ~/.bashrc
finally you get what you want below.
[x#localhost ~]$ tour
2020/10/26 14:37:17 Serving content from /home/x/go/src/golang.org/x/tour
2020/10/26 14:37:17 A browser window should open. If not, please visit http://127.0.0.1:3999
2020/10/26 14:37:18 accepting connection from: 127.0.0.1:55772
Use
~/go/bin/tour
We can write a go script, and run it with the go run.
Some languages can also be started like a script language e.g. kotlinc hello.kt.
That may not be so easy with go with tour. Better use the binary located at ~/go/bin .
Helping Links Writing Scripts with Go:
Go code as a script, gist: Writing Scripts with Go
Related
I'm trying to install my package using go install but I get this error message when running the command go install github.com/JoaoDanielRufino/gcloc/cmd/gcloc#latest:
go install: github.com/JoaoDanielRufino/gcloc/cmd/gcloc#latest: module github.com/JoaoDanielRufino/gcloc#latest found (v1.0.0), but does not contain package github.com/JoaoDanielRufino/gcloc/cmd/gcloc
I want the executable name to be gcloc.
Here is the current source code: https://github.com/JoaoDanielRufino/gcloc
Note: I've already tried go clean -modcache but it didn't work
As the main function of this package isn't on its root, you should pass the directory of the main package on your command.
So, your command will be:
go install -v github.com/JoaoDanielRufino/gcloc/cmd#latest
I came across a similar issue when I was trying to use go install to install the cloudflare/cf-terraforming tool on my machine. The documentation for this tool is not clear on the installation and I had to dig around to get this to work
Basically #Jictyvoo answer above sums it up, if the path is pointing to anything other than directory where the main.go file is sitting I got the error
Command: go install github.com/cloudflare/cf-terraforming#latest v0.8.0#latest
go: github.com/cloudflare/cf-terraforming#latest: module
github.com/cloudflare/cf-terraforming#latest found (v0.8.0), but does not
contain package github.com/cloudflare/cf-terraforming
when I switched to the below it worked fine for me:
Command: go install -v github.com/cloudflare/cf-terraforming/cmd/cf-terraforming#latest
This worked for me after checking the repo and realising that the main.go file was sitting in the cmd/cf-terraforming subdirectory
I'm a beginning go user trying to install the go apache arrow module, so I can run the introductory examples in the user guide. When I try to install the library, I receive the following errors:
$ go install github.com/apache/arrow/go#latest
go: github.com/apache/arrow/go#latest:
module github.com/apache/arrow#latest found (v0.0.0-20220326002331-5bd4d8ec279d),
but does not contain package github.com/apache/arrow/go
$ go install github.com/apache/arrow/go#v7.0.0
go: github.com/apache/arrow/go#v7.0.0: github.com/apache/arrow/go#v7.0.0:
invalid version: go/go.mod has post-v7 module path
"github.com/apache/arrow/go/v7" at revision go/v7.0.0
I've been able to install other go packages successfully, so I don't understand why this install is erroring out.
What is the correct invocation of "go install" to install apache arrow?
There is an easy way: assume you have done go mod init in your local project, you can start to write a file and import github.com/apache/arrow/go/v8 Like this example:
https://github.com/apache/arrow/blob/master/go/arrow/_examples/helloworld/main.go
Now you can easily do
go mod tidy && go mod vendor
And the tool should recognize the imports to download and vendorize.
Or you can do explicitly in your project dir,
go get -u github.com/apache/arrow/go/v8
then run the mod tidy and mod vendor
godoc command doesn't work on my system (I'using Linux Mint 20 Ulyana).
I've just tried this procedure:
install godoc with following command:
go get golang.org/x/tools/cmd/godoc
Start godoc server:
godoc -http=:6060
The result is:
bash: godoc: command not found
I'm using this go version go version go1.15 linux/amd64
And this is my PATH variable /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/usr/local/go/bin
All other go commands (go build, go run and so on) work correctly.
What can I do to make godoc command work?
I have a different issue. As of 1.18, you must now run
go install golang.org/x/tools/cmd/godoc
This is because go get is deprecated for
Starting in Go 1.17, installing executables with go get is deprecated. go install may be used instead. In Go 1.18, go get will no longer build packages...
In other words, go get in 1.18 and beyond will no longer install executables. Use go install.
Step - 1: Check if godoc package is installed
Make sure you can run godoc using this command:
$GOPATH/bin/godoc -http=:6060
Step - 2: Install godoc package
If you don't see any error then go to Step - 4 else if you can see this error No such file or directory then you have to get the godoc package first by using this command:
go get golang.org/x/tools/cmd/godoc
It will take some time to install.
Step - 3: Try godoc command
Try this command
godoc --help
if this command ran successfully then you are done and nothing else to do else if you are still getting any errors follow the Step - 4 and if you still fail please check if you have defined the $GOPATH variable correctly
Step - 4: Add path variable
Add $GOPATH/bin to your PATH variable by using this command:
export PATH="$GOPATH/bin:$PATH"
Try Step - 3 now.
Add $GOPATH/bin to your PATH variable. Executables, like godoc, are installed to $GOPATH/bin.
export PATH="$GOPATH/bin:$PATH"
godoc -http=:6060
Below is what I did on macos, it should work on linux as well.
Add this to your ~/.bashrc or ~/.zshrc:
export GOPATH=$HOME/go # or somewhere else
export GOROOT=/usr/local/opt/go/libexec
export PATH=$PATH:$GOPATH/bin
export PATH=$PATH:$GOROOT/bin
Reload your terminal and run:
mkdir -p $GOPATH $GOPATH/src $GOPATH/pkg $GOPATH/bin
go install golang.org/x/tools/cmd/godoc#latest
godoc -http=localhost:6060
Then you can open your browser at localhost:6060 to RTFM ;-)
I'm using macOS, for me $GOPATH was not configured and the path I found the installed package was $HOME/go/bin/godoc. Check out go help gopath for more information regarding this.
I'm trying out Go for the first time. I was following these docs and wanted to run the go tour locally, but I haven't figured out how to get it to work.
Where is the tool "tour" supposed to be found?
I'm on OSX 10.11.3, and I installed Go via Homebrew
my Go entries in .zshrc
export GOPATH=$HOME/code/Go
export GOROOT=/usr/local/opt/go/libexec
export PATH=$PATH:$GOPATH/bin
export PATH=$PATH:$GOROOT/bin
tour is not installed by default with an installation of go. You need to go get it:
go get golang.org/x/tour/gotour
Docs: https://github.com/golang/tour/
I had a problem too. This's my solution, on OSX let try
gotour
With version go1.8.1 darwin/amd64
It works for me using go1.4, but not with go1.7. If you just run go tool, it gives you a list of known tools. They seem to have removed it from tools.
$ gvm use go1.7
$ go tool
addr2line
api
asm
cgo
compile
cover
dist
doc
fix
link
nm
objdump
pack
pprof
trace
vet
yacc
$ gym use go1.4
$ go tool
6a
6c
6g
6l
addr2line
cgo
cover
dist
fix
nm
objdump
pack
pprof
tour # <--- here
vet
yacc
Firstly, it is no longer gotour. And secondly, for the time being, the tour package is located at: golang.org/x/website/tour as opposed to what A Tour of Go Welcome Page says.
So, at least for now:
The correct way to get tour is:
go get golang.org/x/website/tour
Or,
go install golang.org/x/website/tour#latest
After which you can run the command in the terminal:
$ tour
2021/06/22 17:46:48 Serving content from /home/user/go/pkg/mod/golang.org/x/website/tour#v0.0.0-20210616181959-e0d934b43647
2021/06/22 17:46:48 A browser window should open. If not, please visit http://127.0.0.1:3999
2021/06/22 17:46:52 accepting connection from: 127.0.0.1:33192
To find out where it has been installed, you can do which tour:
$ which tour
/home/user/go/bin//tour
reference
Because of changes in Go package management and introduction of modules, this has changed since the original question. Thus, for future reference (as this is the first Google result), if you have not configured GOPATH environment variable
go get golang.org/x/tour
$HOME/go/bin/tour
Note that the executable is called "tour" instead of "gotour".
Source (with full explanation of why and details): https://stephencharlesweiss.com/getting-going-with-golang/
I've got:
golang.org/x/tour/gotour has moved to golang.org/x/tour
So, this works for me:
go get golang.org/x/tour
then:
tour
When you install go, tour is not installed by default. You need to do a go get golang.org/x/tour/gotour. This downloads gotour in your workspace.
If you configured your PATH properly, gotour command from anywhere in the terminal will open up your browser, but if PATH is not configured properly, do a
$GOPATH/bin/gotour
This command can be used from anywhere in your command line and it opens tour in your default browser
http://whipperstacker.com/2015/09/27/how-to-run-the-go-tour-locally/
https://github.com/golang/tour/blob/master/README.md
Once gotour is installed, it’s executable like other executables are typically stored in the bin directory of your workspace. Inside the bin directory ./gotour will invoke or start gotour, elsewhere the gotour will need to be preceded by a path to where the executable is located. In other words $GOPATH/bin/gotour will invoke or start gotour when you are not inside the bin directory.
I want to install json2csv using go get github.com/jehiah/json2csv but I receive this error:
package github.com/jehiah/json2csv: cannot download, $GOPATH not set. For more details see: go help go path
Any help on how to fix this on MacOS?
[Update: as of Go 1.8, GOPATH defaults to $HOME/go, but you may still find this useful if you want to understand the GOPATH layout, customize it, etc.]
The official Go site discusses GOPATH and how to lay out a workspace directory.
export GOPATH="$HOME/your-workspace-dir/" -- run it in your shell, then add it to ~/.bashrc or equivalent so it will be set for you in the future. Go will install packages under src/, bin/, and pkg/, subdirectories there. You'll want to put your own packages somewhere under $GOPATH/src, like $GOPATH/src/github.com/myusername/ if you want to publish to GitHub. You'll also probably want export PATH=$PATH:$GOPATH/bin in your .bashrc so you can run compiled programs under $GOPATH.
Optionally, via Rob Pike, you can also set CDPATH so it's faster to cd to package dirs in bash: export CDPATH=.:$GOPATH/src/github.com:$GOPATH/src/golang.org/x means you can just type cd net/html instead of cd $GOPATH/src/golang.org/x/net/html.
Keith Rarick notes you can set GOPATH=$HOME to put Go's src/, pkg/ and bin/ directories right under your homedir. That can be nice (for instance, you might already have $HOME/bin in your path) but of course some folks use multiple workspaces, etc.
This one worked
Setting up Go development environment on Ubuntu, and how to fix $GOPATH / $GOROOT
Steps
mkdir ~/go
Set $GOPATH in .bashrc,
export GOPATH=~/go
export PATH=$PATH:$GOPATH/bin
Using brew
I installed it using brew.
$ brew install go
When it was done if you run this brew command it'll show the following info:
$ brew info go
go: stable 1.4.2 (bottled), HEAD
Go programming environment
https://golang.org
/usr/local/Cellar/go/1.4.2 (4676 files, 158M) *
Poured from bottle
From: https://github.com/Homebrew/homebrew/blob/master/Library/Formula/go.rb
==> Options
--with-cc-all
Build with cross-compilers and runtime support for all supported platforms
--with-cc-common
Build with cross-compilers and runtime support for darwin, linux and windows
--without-cgo
Build without cgo
--without-godoc
godoc will not be installed for you
--without-vet
vet will not be installed for you
--HEAD
Install HEAD version
==> Caveats
As of go 1.2, a valid GOPATH is required to use the `go get` command:
https://golang.org/doc/code.html#GOPATH
You may wish to add the GOROOT-based install location to your PATH:
export PATH=$PATH:/usr/local/opt/go/libexec/bin
The important pieces there are these lines:
/usr/local/Cellar/go/1.4.2 (4676 files, 158M) *
export PATH=$PATH:/usr/local/opt/go/libexec/bin
Setting up GO's environment
That shows where GO was installed. We need to do the following to setup GO's environment:
$ export PATH=$PATH:/usr/local/opt/go/libexec/bin
$ export GOPATH=/usr/local/opt/go/bin
You can then check using GO to see if it's configured properly:
$ go env
GOARCH="amd64"
GOBIN=""
GOCHAR="6"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/usr/local/opt/go/bin"
GORACE=""
GOROOT="/usr/local/Cellar/go/1.4.2/libexec"
GOTOOLDIR="/usr/local/Cellar/go/1.4.2/libexec/pkg/tool/darwin_amd64"
CC="clang"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fno-common"
CXX="clang++"
CGO_ENABLED="1"
Setting up json2csv
Looks good, so lets install json2csv:
$ go get github.com/jehiah/json2csv
$
What just happened? It installed it. You can check like this:
$ $ ls -l $GOPATH/bin
total 5248
-rwxr-xr-x 1 sammingolelli staff 2686320 Jun 9 12:28 json2csv
OK, so why can't I type json2csv in my shell? That's because the /bin directory under $GOPATH isn't on your $PATH.
$ type -f json2csv
-bash: type: json2csv: not found
So let's temporarily add it:
$ export PATH=$GOPATH/bin:$PATH
And re-check:
$ type -f json2csv
json2csv is hashed (/usr/local/opt/go/bin/bin/json2csv)
Now it's there:
$ json2csv --help
Usage of json2csv:
-d=",": delimiter used for output values
-i="": /path/to/input.json (optional; default is stdin)
-k=[]: fields to output
-o="": /path/to/output.json (optional; default is stdout)
-p=false: prints header to output
-v=false: verbose output (to stderr)
-version=false: print version string
Add the modifications we've made to $PATH and $GOPATH to your $HOME/.bash_profile to make them persist between reboots.
Watch a Video
In general, I always recommend this official video from Go to get a quick overview on the matter:
http://www.youtube.com/watch?v=XCsL89YtqCs
It's easier to be shown than to be told.
#jwfearn paraphrased the important part of the video:
export GOPATH="${HOME}/gocode"; export PATH="${PATH}:${GOPATH}/bin"; mkdir -p "${GOPATH}"
I found easier to do it like this:
export GOROOT=$HOME/go
export GOPATH=$GOROOT/bin
export PATH=$PATH:$GOPATH
For MAC this worked well for me.
sudo nano /etc/bashrc
and add the below at the end of the file
export PATH=$PATH:/usr/local/opt/go/libexec/bin
export GOPATH=/usr/local/opt/go/bin
This should fix the problem. Try opening a new terminal and echo $GOPATH you should see the correct value.
(for MAC)
I tried all these answers and, for some still unknown reason, none of them worked.
I had to "force feed" the GOPATH by setting the environment variable per every command that required it. For example:
sudo env GOPATH=$HOME/goWorkDirectory go build ...
Even glide was giving me the GOPATH not set error. Resolved it, again, by "force feeding":
I tried all these answers and, for some still unknown reason, none of them worked.
I had to "force feed" the GOPATH by setting the environment variable per every command that required it.
sudo env GOPATH=$HOME/goWorkDirectory glide install
Hope this helps someone.
Your $GOROOT should not be set up.
You $GOPATH should be set to $HOME/go by typing export $GOPATH=$HOME/go
Please type export GOROOT="" to fix your problem.
Just do export GOPATH="/whatever/you/like/your/GOPATH/to/be".
If you run into this problem after having $GOPATH set up, it may be because you're running it with an unsupported shell. I was using fish and it did not work, launching it with bash worked fine.
You can use the "export" solution just like what other guys have suggested. I'd like to provide you with another solution for permanent convenience: you can use any path as GOPATH when running Go commands.
Firstly, you need to download a small tool named gost : https://github.com/byte16/gost/releases . If you use ubuntu, you can download the linux version(https://github.com/byte16/gost/releases/download/v0.1.0/gost_linux_amd64.tar.gz).
Then you need to run the commands below to unpack it :
$ cd /path/to/your/download/directory
$ tar -xvf gost_linux_amd64.tar.gz
You would get an executable gost. You can move it to /usr/local/bin for convenient use:
$ sudo mv gost /usr/local/bin
Run the command below to add the path you want to use as GOPATH into the pathspace gost maintains. It is required to give the path a name which you would use later.
$ gost add foo /home/foobar/bar # 'foo' is the name and '/home/foobar/bar' is the path
Run any Go command you want in the format:
gost goCommand [-p {pathName}] -- [goFlags...] [goArgs...]
For example, you want to run go get github.com/go-sql-driver/mysql with /home/foobar/bar as the GOPATH, just do it as below:
$ gost get -p foo -- github.com/go-sql-driver/mysql # 'foo' is the name you give to the path above.
It would help you to set the GOPATH and run the command. But remember that you have added the path into gost's pathspace. If you are under any level of subdirectories of /home/foobar/bar, you can even just run the command below which would do the same thing for short :
$ gost get -- github.com/go-sql-driver/mysql
gost is a Simple Tool of Go which can help you to manage GOPATHs and run Go commands. For more details about how to use it to run other Go commands, you can just run gost help goCmdName. For example you want to know more about install, just type words below in:
$ gost help install
You can also find more details in the README of the project: https://github.com/byte16/gost/blob/master/README.md
Run 'go env' and see where your GOPATH is currently pointing towards. If you change to that directory, your 'go get..etc' command should work.
This problem occured to me in raspberry pi. I had logged in through VNC client
The problem persisted despite setting and exporting the GOPATH.
Then Ran the "go get" command without sudo and it worked perfectly.
I am using vim to edit my .bashrc file but you code use a gui editor such as gedit.
Steps:
Kindly subsitute /path/to/golang/projects below with your actual path location where you will store your golang projects.
Open .bashrc file in vim that is vim ~/.bashrc. Then add below lines at the end of the file.
# Setup Golang Development Environment ::
export GOROOT=/usr/local/go
export PATH=$PATH:$GOROOT/bin
# Third party go libraries will live under "~/golib" directory
export GOPATH="$HOME/golib"
export PATH="$PATH:$GOPATH/bin"
# Where your golang project code lives
export GOPATH=$GOPATH:/path/to/golang/projects
Save the file and type source ~/.bashrc to refresh your terminal session.
Now try getting a package e.g. go get github.com/pilu/fresh and check your ~/golib/bin directory it should have fresh package in it.
Navigate to your /path/to/golang/projects and create three folders in there i.e. bin,src and pkg
Now place your project folder inside /path/to/golang/projects/src e.g. /path/to/golang/projects/src/myfancygolangprojectand you should be good to go. Put all your golang codebase in there mate.
I had to run an application as root (to open a webserver on port 80), this produced the error for me, because the sudo user has a different environment than the normal user, hence GOPATH was not set.
If someone else is having this problem, add -E to the command, this will preserve the user environment.
sudo -E go run main.go
For more infos see discussion here: Google Groups – GOPATH Problem