Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Help setting up Go workspace on Arch Linux
New Posts  All Forums:Forum Nav:

Help setting up Go workspace on Arch Linux

post #1 of 9
Thread Starter 
Hello,

I'm trying to setup my workspace for developing Go applications but am apparently unable to get the workspace setup right. I installed the go package from the repos (it's go version 1.1) and performed the following commands in accordance with http://golang.org/doc/code.html but to no avail. I'm getting an error every time I run go install. Here's what I did to setup:
Code:
nixalot:~/go/src$ cd
nixalot:~$ mkdir -p $HOME/go
nixalot:~$ export GOPATH=$HOME/go
nixalot:~$ cd $GOPATH
nixalot:~/go$ mkdir -p bin src pkg
nixalot:~/go$ cd src
nixalot:~/go/src$ mkdir hello
nixalot:~/go/src$ cd hello
nixalot:~/go/src/hello$ vim hello.go
nixalot:~/go/src/hello$ go run hello.go
Hello OCN!
nixalot:~/go/src/hello$ go install hello.go
go install: no install location for directory /home/matt/go/src/hello outside GOPATH

Here's the contents of hello.go
Code:
package main

import "fmt"

func main() {
    fmt.Println("Hello OCN!")
}

I don't get what the "no install location" error is trying to tell me apparently since according to the document linked to above it should work. I've done some googling trying to fix this and I haven't found anything useful. It all says to set $GOPATH but I obviously did and i'm still getting the error. What am I missing?
post #2 of 9
Thread Starter 
Ok I figured it out. Apparently I wasn't reading the document right. I watched the screencast at http://www.youtube.com/watch?v=XCsL89YtqCs and changed the above
Code:
go install hello.go
to
Code:
go install
and it worked. I guess you aren't supposed to specifically name the go source files you want to install, just the directory containing them.
post #3 of 9
Why are you even installing them? Surely you want to be building them instead:
Code:
go build hello

Incidentally, you can build against individual go files (eg go build hello.go would work). The problem with specifying individual files is that if your project has several .go files (eg one that deals with the UI and one with the internal logic of the program), then you specify the src folder name and every .go file in that folder will be included (you don't include source files like you would with C or Perl. Instead you include sources as it's assumed that anything within that source is required as part of your package. It's a bit of a weird concept to wrap your head around at first, but I think it actually makes a great deal of sense in practice.
post #4 of 9
Thread Starter 
Quote:
Originally Posted by Plan9 View Post

Why are you even installing them? Surely you want to be building them instead:
Code:
go build hello

Incidentally, you can build against individual go files (eg go build hello.go would work). The problem with specifying individual files is that if your project has several .go files (eg one that deals with the UI and one with the internal logic of the program), then you specify the src folder name and every .go file in that folder will be included (you don't include source files like you would with C or Perl. Instead you include sources as it's assumed that anything within that source is required as part of your package. It's a bit of a weird concept to wrap your head around at first, but I think it actually makes a great deal of sense in practice.

It isn't listed in my original post but I used
Code:
go build
to ensure the code compiled before I ran "go install" on it. The above was specifically an issue with
Code:
go install
or more accurately the issue was with me. According to the documentation on golang.org "go build" disgards the results unless you're building a single file, but either way it doesn't install the file anywhere (i.e the bin directory of your $GOPATH). From the site:
Quote:
Go is a tool for managing Go source code.

Usage:
Code:
go command [arguments]
The commands are:
Code:
build       compile packages and dependencies
clean       remove object files
doc         run godoc on package sources
env         print Go environment information
fix         run go tool fix on packages
fmt         run gofmt on package sources
get         download and install packages and dependencies
install     compile and install packages and dependencies
list        list packages
run         compile and run Go program
test        test packages
tool        run specified go tool
version     print Go version
vet         run go tool vet on packages
post #5 of 9
Quote:
Originally Posted by Nixalot View Post

It isn't listed in my original post but I used
Code:
go build
to ensure the code compiled before I ran "go install" on it. The above was specifically an issue with
Code:
go install
or more accurately the issue was with me. According to the documentation on golang.org "go build" disgards the results unless you're building a single file, but either way it doesn't install the file anywhere (i.e the bin directory of your $GOPATH). From the site:

I can assure you that go build doesn't disregard the file. I use it pretty much daily so I should know. wink.gif
Code:
go build hello
Will compile hello and leave the resulting binary in your working folder. So if you're sat in /home/nixalot, your GOPATH=$HOME/go, and you're project is $GOPATH/src/hello (ie all your project files are $GOPATH/src/hello/*.go) then when you run go build hello the resulting binary will be /home/nixalot/hello (likewise, if you cd to /cat/plays/piano and then run go build hello, the resulting binary will be compiled to /cat/plays/piano/hello)

What I tend to do is have a few environmental files in $GOPATH/bin (eg env_pi, env_linux64, etc) so if I'm compiling a project for a specific platform, I will cd to the bin directory and then run . env_pi to set up Go's environment to compile a binary for the Raspberry Pi, then go build project-name and just copy the resulting binary onto my Raspberry Pi (it keeps all the file system hierarchy clean, my projects organised and means I can cross compile for other platforms quickly and painlessly).

The difference between go build and go install is the same as make and make install when working with C++ make files. ie the former one compiles to your current / working directory and the latter will install the resulting binary into a pre-assigned install location (eg your bin directory or /usr/local/project-name). Since you're just wanting to compile and test your code, you do not want to run go install.
Edited by Plan9 - 5/21/13 at 3:02am
post #6 of 9
Thread Starter 
I see. I'm just learning go now because it looks interesting and I'm bored because I feel like I've got Python down pretty good since I use it every day at work. My confusion comes from the following description on http://golang.org/cmd/go/
Quote:
Build compiles the packages named by the import paths, along with their dependencies, but it does not install the results.

If the arguments are a list of .go files, build treats them as a list of source files specifying a single package.

When the command line specifies a single main package, build writes the resulting executable to output. Otherwise build compiles the packages but discards the results, serving only as a check that the packages can be built.

The -o flag specifies the output file name. If not specified, the output file name depends on the arguments and derives from the name of the package, such as p.a for package p, unless p is 'main'. If the package is main and file names are provided, the file name derives from the first file name mentioned, such as f1 for 'go build f1.go f2.go'; with no files provided ('go build'), the output file name is the base name of the containing directory.

Apparently I misunderstood it. I missed the
Quote:
When the command line specifies a single main package, build writes the resulting executable to output.
apparently. Thanks for clearing that up for me.
post #7 of 9
I just knocked up the shortest possible program I could to demonstrate the build process (minus setting the envs, but you already have that bit nailed anyway)
Code:
planet-express /home/plan9/dev/go/bin> ls
planet-express /home/plan9/dev/go/bin> cat $GOPATH/src/toldyouso/*
package main

func main() {
    println("😋")
}
planet-express /home/plan9/dev/go/bin> go build toldyouso
planet-express /home/plan9/dev/go/bin> ls
toldyouso
planet-express /home/plan9/dev/go/bin> ./toldyouso 
😋

Incidentally, that box is meant to be a unicode character for "tongue.gif" (just in case your browser doesn't render some of the more exotic unicode characters).

and to prove build compiles to your current directory:
Code:
planet-express /home/plan9/dev/go/bin> sudo mkdir -p /cat/plays/piano
planet-express /home/plan9/dev/go/bin> sudo chown `whoami` !$
sudo chown `whoami` /cat/plays/piano
planet-express /home/plan9/dev/go/bin> cd !$
cd /cat/plays/piano
planet-express /cat/plays/piano> !?build
go build toldyouso
planet-express /cat/plays/piano> ls
toldyouso
planet-express /cat/plays/piano> ./toldyouso 
😋
post #8 of 9
Quote:
Originally Posted by Nixalot View Post

I see. I'm just learning go now because it looks interesting and I'm bored because I feel like I've got Python down pretty good since I use it every day at work. My confusion comes from the following description on http://golang.org/cmd/go/
Apparently I misunderstood it. I missed the
apparently. Thanks for clearing that up for me.

To be honest, I never went through the doc for the go command. I literally just ran go --help then bashed a few options until it worked (I'm useless at following instructions so breaking things is usually the best way I learn laugher.gif)

edit: also please excuse the sarcastic tone of the program above. It was intended to be witty but in hindsight I think it sounds more rude redface.gif
Edited by Plan9 - 5/21/13 at 3:34am
post #9 of 9
Thread Starter 
Quote:
Originally Posted by Plan9 View Post

To be honest, I never went through the doc for the go command. I literally just ran go --help then bashed a few options until it worked (I'm useless at following instructions so breaking things is usually the best way I learn laugher.gif)

edit: also please excuse the sarcastic tone of the program above. It was intended to be witty but in hindsight I think it sounds more rude redface.gif

No big. I understand.
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Help setting up Go workspace on Arch Linux