2

I'm trying to run my golang program whenever I log into OSX. I'm trying the following script with Automator:

#!/bin/sh
export GOPATH=/Volumes/DTSE9/worker
go run /Volumes/worker/worker.go

Whenever I run this with Automator, it tells me go: command not found

4
  • So go isn't in the PATH. You either add it to the PATH, or us a full path. Commented Mar 17, 2017 at 19:14
  • @JimB Do you mean add export GOROOT=/usr/local/go/bin/go above the first export? Just tried that and it doesn't work. Commented Mar 17, 2017 at 19:18
  • No, don't set GOROOT (and that's not a valid value for GOROOT anyway). If /usr/local/go/bin isn't in your path, you need to add it to your PATH, or use /usr/local/go/bin/go run Commented Mar 17, 2017 at 19:20
  • 1
    But why not just compile the program and run the executable? It will run faster and won't depend on the state of GOPATH. Commented Mar 17, 2017 at 19:21

1 Answer 1

4

Create a file like say file.go and it's content should look like:

///path-to/bin/go run $0 $@; exit $?

package main

func main() {
    println("Hello World!")
}

Then run chmod +x file.go and then you can execute it as ./file.go p1 p2.

Edit: This works on Ubuntu. I did not saw that OSX was in question.

This is another version of the first line which does not require the path to the go command (tested on zsh):

///$(which go) run $0 $@; exit $?
Sign up to request clarification or add additional context in comments.

10 Comments

lol, really taking the term "go script" literally, but it is a neat hack ;). It should run fine on macOS too.
Just tried that and got: go: unknown subcommand "./main.go" Run 'go help' for usage.
You do not need to call go command anymore - just $ ./main.go. And I'm using it on Ubuntu; though I'm not familiar with OSX, but since it's a POSIX compatible OS (& as I understand, has bash), this should work on OSX too.
@KavehShahbazian yeah, that's what I did. $ ./main.go and I got the above error message.
Then it's something about OSX that I'm not aware of (but a guess is at the first line instead of /path-to/go run you just have /path-to/go).
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.