Set working directory for go run

7,357 views
Skip to first unread message

Benjamin Savs

unread,
Jun 27, 2014, 11:02:58 AM6/27/14
to golan...@googlegroups.com
Is it possible to pass a parameter to go run in order to change the working directory?
For me it would be usefull, so that files I read/write don't are relative to the source.go I'm running but something else.

James Bardin

unread,
Jun 27, 2014, 11:50:46 AM6/27/14
to golan...@googlegroups.com

"go run" is mostly a convenience, and isn't meant to extend very far beyond a .go file or two.

As soon a "run" can't do what you want, it's time to use "build" or "install"

Dave Cheney

unread,
Jun 28, 2014, 12:30:40 AM6/28/14
to golan...@googlegroups.com


On Saturday, 28 June 2014 01:50:46 UTC+10, James Bardin wrote:

"go run" is mostly a convenience, and isn't meant to extend very far beyond a .go file or two.

As soon a "run" can't do what you want, it's time to use "build" or "install"

+1. What James said. 

Dmitri Shuralyov

unread,
Jun 28, 2014, 1:14:32 AM6/28/14
to golan...@googlegroups.com
Sure, it's important to be able to use "go build" and "go install" comfortably.

But it is possible to achieve what the OP wants; just cd into the directory you want to have as your working directory, and then "go run /full/path/to/your/go_file.go".

Benjamin Savs

unread,
Jun 28, 2014, 8:53:17 AM6/28/14
to golan...@googlegroups.com
Thanks for the ideas!

Benjamin Savs

unread,
Jun 28, 2014, 9:49:26 AM6/28/14
to golan...@googlegroups.com
As a followup question, though.

Is go run not suitable during development, comparable to e.g. pressing F5 in qtcreator or visual studio?

Shawn Milochik

unread,
Jun 28, 2014, 10:11:19 AM6/28/14
to golan...@googlegroups.com
On Sat, Jun 28, 2014 at 9:49 AM, Benjamin Savs <benjam...@gmail.com> wrote:
As a followup question, though.

Is go run not suitable during development, comparable to e.g. pressing F5 in qtcreator or visual studio?


I generally code in vim in a tmux session. I have a bash one-liner or simple bash script running in a pane next to my editor. It loops forever and, depending on what I'm doing, builds and runs every time a file is changed, or kills, builds, and restarts (for a Web app).

You could do it just as easily with  "go run" instead of "go build," but I don't see any advantage beyond not leaving behind a binary you might want to delete later.

Here's a simple example. It keeps a live version of my Web app running while I'm developing it. It's really handy because, unlike Python, I can't break it -- if my code doesn't compile, the old version keeps running until I save something valid. I just use "monkey" as my binary name because it amuses me. It also checks for any updated files in my $GOPATH/src (via ..) because I might be writing code in multiple packages.

#!/usr/bin/env bash

EXE=monkey

while true; do 
    for SRC in $(find .. -name '*.go' -mmin -1); do
        if [ "$SRC" -nt "$EXE" ]; then
            pkill -u $USER -f $EXE
            { go build -o $EXE && echo "built successfully"; } || echo "build failed"
            ./$EXE &
            date
            break
        fi
    done
    sleep 15
done


Henrik Johansson

unread,
Jun 28, 2014, 10:49:56 AM6/28/14
to Sh...@milochik.com, golang-nuts

I do something similar but rely on inotify tools to avoid the busy loop.

It is a very neat and light setup with close to IDE like power but much less hassle.

--
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...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages