Go project skeleton



In this tutorial we are going to create a skeleton of a command line tool for GO. It is required to install GO, if you didn't do it please follow this tutorial to install GO it in your system. To make the tutorial easier we will use a tool that will generate all the directories and files, in particular we will use gcli. Gcli generates a skeleton (codes and its directory structure) you need to start building Command Line Interface (CLI) tool by Golang. Finnally out command will be used to send a message to a channel to slack using a webhook.

Step 1: install gcli

We need to install gcli, please execute this command to have gcli installed in your system:

go get -u github.com/jteeuwen/go-bindata/...
go get -d github.com/tcnksm/gcli
cd $GOPATH/src/github.com/tcnksm/gcli
make install

If everything was successfull, the output should be:

====> Install gcli in /home/leonardo/go/bin ...

Step 2: Generate the files

Let's try the following command to create a new project:

gcli new -F mitchellh_cli -c alertslack projectname

The command above will create a new directory called projectname with the skeleton for a command line made with GO. Let's explore the parameters of the gcli:

  • -F is to specify the Cli framework name. You can check for avaible frameworks with gcli list.
  • -c is used to name the command that will be used, you can add more than one.

The output should be something like this:

  Created /home/username/go/src/github.com/Username/projectname/main.go
  Created /home/username/go/src/github.com/Username/projectname/README.md
  Created /home/username/go/src/github.com/Username/projectname/version.go
  Created /home/username/go/src/github.com/Username/projectname/cli.go
  Created /home/username/go/src/github.com/Username/projectname/CHANGELOG.md
  Created /home/username/go/src/github.com/Username/projectname/commands.go
  Created /home/username/go/src/github.com/Username/projectname/command/manage.go
  Created /home/username/go/src/github.com/Username/projectname/command/version.go
  Created /home/username/go/src/github.com/Username/projectname/command/manage_test.go
  Created /home/username/go/src/github.com/Username/projectname/.gitignore
  Created /home/username/go/src/github.com/Username/projectname/command/meta.go
====> Successfully generated projectname

Go to the new projectname directory and execute:

go build

Step 3: Playing with your new GO command

To make this tutorial a little more interesting we are going to use the new manage command to send a message to a slack channel. For simplicity we will use webhooks, please read the slack webhooks information page for more information (we will not cover how to get a webhook url). First Create a test incoming webhook here, use the generated webhook URL below.

Now open the commands/manage.go file and chnage the Run function to this one:

func (c *ManageCommand) Run(args []string) int {
      fmt.Println("URL:>", url)

      var jsonStr = []byte(`{"text": "This is posted to #bots and comes from *go* command line.", "channel": "#bots", "link_names": 1, "username": "go-bot", "icon_emoji": ":monkey_face:"}`)
      req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonStr))
      req.Header.Set("Content-Type", "application/json")

      client := &http.Client{}
      resp, err := client.Do(req)
      if err != nil {
      defer resp.Body.Close()

      fmt.Println("response Status:", resp.Status)
      fmt.Println("response Headers:", resp.Header)
      body, _ := ioutil.ReadAll(resp.Body)
      fmt.Println("response Body:", string(body))
      return 0

Now everytime you execute:

./projectname alertslack

the message "This is posted to #bots and comes from go command line."