Responses to a question like this tend to be either very detailed, or 
vague.  This one has all the gory details.

I develop under Linux.  I'm happy to use fancy visual tools, but I also do 
a lot of stuff from the command line.

I have a directory GOCODE where I put stuff that I fetch via go get.  I set 
this up In my .profile, and put the bin directory in my path along with the 
Go bin directory:

    GOPATH=$HOME/gocode
    export GOPATH

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

(Under Linux you can create a variable and export it in one command, but 
that doesn't work across all UNIX systems.)

You may want to include your go tools bin directory in the path as well.

With this .profile, once I'm logged in, I have a GOPATH variable containing 
my gocode directory.

I keep each of my Go projects in a separate directory.  In the top level 
directory of each project I create a file setenv.sh containing this:

    if test -z $GOPATH
    then
        GOPATH=`pwd`
        export GOPATH
    else
        GOPATH=$GOPATH:`pwd`
        export GOPATH
    fi

    PATH=`pwd`/bin:$PATH
    export PATH


That notation `pwd` in grave accents, means the current directory, so I can 
just copy this file from one project to another and it will work, as long 
as you run it from the right directory.

To set up GOPATH for a project, I start a command window, change directory 
to the project and run the commands in setenv.sh:

    .  setenv.sh

(Note the "." at the start - see later about that.)  If GOPATH doesn't 
already exist, it's created and contains just the current directory.  If it 
exists, the current directory is added.  Then the local bin directory is 
added to my path.  YOU NEED TO BE IN THE RIGHT DIRECTORY, the top level of 
your project (the one that contains src, pkg, bin etc.)

For example, if my project is $HOME/project1 and my .profile is set up as 
above, then GOPATH would contain something like

    /home/simon/gocode:/home/simon/project1

Now the go command can find my project and anything I've downloaded via "go 
get".

My PATH variable will contain

    /home/simon/project1/bin:(whatever was already in the path)

so any command that I build within this command window is in my path.  In 
my .profile I set up the path to include the go and gocode bin directories, 
so that stuff is in my path too.

Strictly, I don't need the setenv.sh file.  I could just cd to the right 
directory and run this command by hand:

    GOPATH=$GOPATH:`pwd`

As long as you cd to the right directory first.  In fact the main purpose 
of creating the setenv.sh files is to remind me which directory I should be 
in when I run the commands! 

If GOPATH contains a colon-separated list of directories as above, "go get" 
uses the first one in the list.  In my case that will always be my gocode 
directory, because I set it up that way in my .profile, and my setenv.sh 
files ensure that it's kept as the first directory.  That means I can run 
"go get" in any command window and it will always put stuff in the same 
place.

If I run a tool such as liteide from my command window having run 
setenv.sh, it picks up the GOPATH that I set up.

If you use an IDE (liteide, eclipse, intellij or whatever) then you can 
simply set up a local GOPATH variable in the ide.  The technique I describe 
above is not quite so slick, and depends upon you remembering to run the 
commands in setenv.sh before you start working, but if you still want the 
option of running commands from a command window, you need something like 
this.

A note for people who are not Linux shell experts:   setenv.sh looks like a 
shell script, but it isn't.  If you ran it as a shell script it wouldn't 
work.  Any variables that a shell script set up are lost when the shell 
script ends, so your script would set up GOPATH and then discard it.  You 
need to run setenv.sh using the "." command as above.

I've used all sorts of UNIX magic here - shell if commands, running 
commands in grave accents, running commands using "." and so on.  If you 
are working under Windoze, none of this will work, but you can do similar 
stuff using batch files.

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to