Go Help

Go Format

Introduction

Google Go has a recommended Go Programming Style.  My personal habits are already close to this, so why not conform?  Go comes with a utility “gofmt” to read your source file and output reformatted source to their style.  It cleans up indents, resets braces, lines thing up and, in the newer versions, will try to change your punctuation to the latest and greatest.  It even has a search and replace capability.  The Go team seems to also be using it as a source file conversion utility, as the language evolves, so it is a good tool to know.  To use it, open a terminal and change directories to your source file location.  At the prompt enter ‘gofmt your source file name > file name for your formatted source file‘.  We can make this happen with a single keystroke in Gedit!

Documentation about “gofmt” and its options can be found at  ‘$HOME/go/src/cmd/gofmt/doc.go’ .  You can also type  ‘godoc gofmt’  in a terminal.       Note: the shell can find “gofmt” because your environment PATH variable includes $HOME/bin as documented in the Go installation instructions.

Setup

  1. If you have not done so already, go back to the Chapter Plugins and install External Tools.
  2. In Gedit go to Tools>Manage External Tools.

You should see a screen similar to below. Gedit comes with some already made tools, we will talk about some of them in later chapters.

  1. Go to the bottom left of the window and click on the icon just above the help button.  This will create a new tool waiting for input of a name.  Give it a name, I used ‘Go Format’.
  2. Go to the right hand panel and change “#!/bin/sh” to “#!/bin/bash”.  This is not critical, it will work with “#!/bin/sh”, I just prefer the newer bash.  This line specifies what language to use for this script.  Their are identifiers for Python, Perl, etc.
  3. Next, under “#!/bin/bash”, type “gofmt”.

Below the code panel you can set:

Shortcut Key

Just press the key combination you want to use to reformat.  Also, if there is conflict with another tool, click on that tool name, more to the shortcut key field and change it or press backspace to erase the shortcut.

Save

Set this to current document to save your file before it is reformatted.  This will allow to revert back if you don’t like the reformat.

Input

Set this to current document.  This is the input to “gofmt”.

Output

Set this to replace current document.  This is the output from “gofmt” and replaces the current edit in place.  It does not overwrite the saved file, unless your autosave kicks in later and overwrites it.  If you don’t like the reformat, exit with no save and open the original or, as pointed out by Steve on golang-nuts, just hit the ‘undo’ button on your tool bar.   Important Note: It has been pointed out that if “gofmt” fails it will overwrite the current document with the error message.   I am researching good ways to avoid this, see below.  Meanwhile, if you have this problem, you can set output to create new document to be safe.  It will send the output to a new tab, where you can check it.

Applicability

Choose for your situation.

Languages

Above the Close Button, you can make your tool language specific.  If you installed the syntax highlighter previously, Go will come up as one of your language choices.  This creates a new language category or adds your tool to an existing one in the left pane.  You can set your new Tool to Go.  If you set it to ‘all languages’, then it will always come up on the Tools>External Tools drop down box.  I changed the built in Tools ‘Build’ and the ‘Remove Trailing Spaces’ to C.  Now they don’t come up on the Tools>External Tools when I am editing Go.

Close Window

TEST

Open Gedit.  Enter some source code.  Press your Go Format shortcut key, or go to the menu and go to Tools>External Tools>Go Format.  You should see the code in your edit window reformatted.  If you like the reformat, save it. 

Note: I recommend that you try Go Format V2,  it solves the shortcomings of this simple example.

Create a free website or blog at WordPress.com.

%d bloggers like this: