-1

I am installing go in Ubuntu 14.04 and believe I have my GOPATH$ set right and can't seem to run this go install hello.go file. I got rid of any path errors that I encountered first, but I still am not seeing a successful run

Go is installed in /etc/go

$ go env
GOARCH="amd64"
GOBIN=""
GOCHAR="6"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/etc/go/packages"
GORACE=""
GOROOT="/etc/go"
GOTOOLDIR="/etc/go/pkg/tool/linux_amd64"
CC="gcc"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0"
CXX="g++"
CGO_ENABLED="1"

And here I made the file, edited via nano to copy the hello world example, but when I run it, nothing happens...

$ mkdir -p $GOPATH/src/github.com/hackg
$ nano $GOPATH/src/github.com/hackg/hello/hello.go
$ go install github.com/hackg/hello
$

QUESTION - it is supposed to display hello world there, proving Go is building files properly, but all I get is a fresh terminal prompt ready for a new command, no HELLO WORLD

I tried looking at other stackoverflow posts with no luck - ex Go, Golang : does not make sense that I have to have files before import

Community
  • 1
  • 1
hackg
  • 95
  • 1
  • 6

4 Answers4

4

No, that command won't run your program; go install just made a binary you can run with $GOPATH/bin/hello. You can add to your ~/.bashrc or equivalent the line export PATH=$GOPATH/bin:$PATH, and open a new shell (like by closing and reopening your terminal program), so you can call it with just hello. go running the file would compile and run, but it's reasonable to start with go install because that's what you're going to use for "real" programs as opposed to quick tests.

(Sounds like you're on the right track with a GOPATH and all, but for setting up a Go environment generally you might find this question helpful.)

Community
  • 1
  • 1
twotwotwo
  • 28,310
  • 8
  • 69
  • 56
1

I did the following and it works fine for me. You may try it:-

Run the following command in your terminal:-

sudo apt-get install golang

This will install golang in your system

Add the following lines in your .bashrc file:-

export GOPATH=$HOME/go
export PATH=$PATH:$GOROOT/bin:$GOPATH/bin

Now, close and open your shell. And run the following command to check the version of go installed in your system:-

go version

Suppose hello.go is your file containing hello world program, then do the following:-

go run hello.go

Hope it helps.

a3.14_Infinity
  • 5,653
  • 7
  • 42
  • 66
0

I figured it out, I thought it would run the command, not just install it.

all I had to do once installed was type hello and hit enter

$ hello
hello, world

I suppose I didn't read the directions well. thanks twotwotwo

hackg
  • 95
  • 1
  • 6
0

This gives instructions to get your hello world example working.

To compile and install packages and dependencies:

go install github.com/user/hello

It will put an executable command named hello (or hello.exe) inside the bin directory of your workspace.

To build go program into binaries:

go build github.com/user/hello

To run:

$ $GOPATH/bin/hello
hello, world