Creating a Go module

Dan Corin April 22, 2016
Source

Creating a Go module

We're going to create a CLI tool for sending a message to a channel in Slack using the command line. This post is similar to my earlier post: Creating an Elixir Module. We'll be using the chat.postMessage Slack API endpoint. Also, make sure you have a Slack API token.

Our CLI syntax will be:

First, make sure you have your $GOPATH set properly.

Next, set your Slack token as an environment variable:

Typically, developers will use one folder for all of their Go code, and create new folders within for each new project. My structure looks like this:

Make a folder called slack within src, then inside that folder create the following files and folders as well:

Using the builtin Go command line parser flag, we will write our main.go file to parse the message and channel from the command line.

slack/main.go

This sets us up with a CLI parser which will take our provided arguments and pass them to the SendMsg function, which we will define now.

Make sure you have set the environment variable SLACK_TOKEN with your token.

slack/api/slack.go

With these two files written, we can go the to slack folder and test the program:

If all goes well, you will see the output:

and the message will show up from you to slackbot in the Slack app.

To build the program as a standalone, distributable binary run:

This creates a binary file called slack in the folder, which can be run with:

That's it!

Thanks to Hans Li for the help with testing!

Discussion in the ATmosphere

Loading comments...