Jump to content

Writing workflows with go help


Recommended Posts

I am learning go now as it is quite an exciting language. The thing is that I can't really figure out what I should set my $GOPATH to when developing workflows. 

 

For example, I created a new workflow and I wanted to use this library : https://github.com/jason0x43/go-alfred

 

I then do : 

 

OUsQqEe.png

 

However I get the issue that my $GOPATH is not set. From what I have read, $GOPATH is where one keeps all his go code but I just want to use this library only in this specific library. I was thinking of symlinking it with Deanishe's script but I've read that it may cause some big problems. 

 

What can I do to fix these issues. I know there are a few people here who have tried developing their workflows in go language. I would love to hear how you do it. Thank you.

 

Link to comment

Set $GOPATH to $HOME. Make sure you have ~/bin, ~/src and ~/pkg directories.

 

That way, anything you go install will be put in ~/bin, which should be on your PATH.

 

That should work for private development, but what about distributing workflows? I don't know Go. Is there a way to build a static executable?

Link to comment

Set $GOPATH to $HOME. Make sure you have ~/bin, ~/src and ~/pkg directories.

 

That way, anything you go install will be put in ~/bin, which should be on your PATH.

 

 

I have added these two lines two my zshrc (https://github.com/nikitavoloboev/dotfiles/blob/master/zsh/zshrc#L12) as I want to have these go folders not be in my home folder but a dedicated folder for go. In theory it should work the same. 

 

I then just have these folders in there : 

 

44fyuTg.png

 

Where 'dev' will be the folder where I will actually write the code in. I've read through this article (http://gowithconfidence.tumblr.com/post/118493925546/getting-started-with-go-workspaces) about go workspaces to make more sense of it and I think this should work. 

 

However after all this I am still not able to run my go install command in the directory with my workflow : 

 

Y91UfVS.png

 

 

Did I make some mistake? Do I have to install the library first with some other command? 

 

It seems so complicated :(

 

I tried to make something like this : 

 

eRfzsGy.png

 

And in Alfred preferences, I have : 

 

r8syrP3.png

 

 

Where the script filter is : 

 

gVOqq2Z.png

 

 

I would assume then, that entering qq, pressing enter, will display hello to the prompt but that is not the case. I really wish someone did a guide of how one can start developing workflows for Alfred with go. I guess the reason for that might be that Alfred doesn't accept fmt.Println output but for some reason console.log("hello") worked when I tried running a node workflow. I guess I should stick with python.

Edited by nikivi
Link to comment

You have it all wrong.
 
Go is a compiled language. You do not run your workflow with go run main.go.

 

go run is a convenience when developing that compiles your program (in some temp directory) then runs it. It will crash and burn in Alfred because GOPATH isn't set.
 
Instead, you compile your Go program with go build and run the resulting executable program from your workflow.
 
GOPATH and GOROOT are just for development. GOPATH tells Go where to install libraries and binaries, and where to look for them when you import them.
 
Before you can import any libraries, you have to go get them first, so they (and their dependencies) are available locally.
 
Once your program is compiled, none of that matters. Go programs are always statically compiled, so it will run on any Mac regardless of whether they have Go installed.
 

Did I make some mistake? Do I have to install the library first with some other command?


You need to go get ... any libraries you want to use.
 

It seems so complicated :(
 
I would assume then, that entering qq, pressing enter, will display hello to the prompt but that is not the case. I really wish someone did a guide of how one can start developing workflows for Alfred with go. I guess the reason for that might be that Alfred doesn't accept fmt.Println output but for some reason console.log("hello") worked when I tried running a node workflow. I guess I should stick with python.


fmt.Println() doesn't work because it writes to STDOUT, whereas console.log() writes to STDERR. If you write anything but JSON/XML to STDOUT, your Script Filter won't work. You need fmt.Fprintln(os.Stderr, ...), though log.Println() is probably a better choice.
 
The reason it's "complicated" is because you're trying to figure out two things at the same time. How Alfred/workflows work, and how to program. That is to say, you don't know what you need to do, nor how to do it. That's a hard place to be.

 

My advice is to pick one language (Python or Ruby) and learn it. Read Dive into Python and How to Think Like a Computer Scientist (I'm sure there are Ruby equivalents). Go is a great language, but not a great first language. In particular, almost everything written about it (docs, tutorials, books etc.) assumes you're an experienced coder and know what things like mutexes and pointers are.

Start by looking at other workflows to see how they do stuff, and try adapting them.

Edited by deanishe
Link to comment

Thank you so much for the detailed answer. You are right, figuring out two things at once is quite hard. I actually started reading through Dive into Python 3 book now. I wanted to actually write, save and test the code I write through Alfred interface straight away as I think it is quite convenient. If you do not mind, can you take a look at this thread, and tell me what the problem is with it (http://www.alfredforum.com/topic/9266-can-someone-help-me-understand-why-this-doesnt-work/)? I think it shouldn't be hard to display the output of my script like that but perhaps I again am misunderstanding some things. Thank you again for your answer.

Link to comment

If you want to learn go development on the Mac, read my tutorial:  http://computers.tutsplus.com/tutorials/start-developing-with-go-on-the-mac--cms-21251

 

I have a go library for making Alfred workflows:  https://github.com/raguay/goAlfred

 

And, I show how to create a markdown powered, go based web server: http://code.tutsplus.com/tutorials/building-a-cms-gopress--cms-25073

 

But, deanishe is right. Go is a compiled language and doesn't play as an interpreted language too well. They did have a go interpreter, but it has fallen out of use. But, you can automate compiling if you set the two mentioned environment variables in your workflow, but I would not recommend it as it would be slow. I always compile inside the workflow and test the compiled code in Alfred. Not too hard.

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...