Zeus + goimports

Find Tips and tricks on how to better use the Zeus IDE. Feel free to post your own tips but please do not post bug reports, feature requests or questions here.
Post Reply
jussij
Site Admin
Posts: 2650
Joined: Fri Aug 13, 2004 5:10 pm

Zeus + goimports

Post by jussij »

It is already easy enough to configure Zeus to run gofmt such that Zeus auto-formats Go code on any file save action.

For more details on this refer to the Go Language Auto-formatting on Save topic.

The goimports utility is drop in replacement for gofmt and like gofmt it formats the code, but this utility also adds/removes imports as required.

So How Does it Work?
The Zeus file save trigger allows a macro to be attached to any file save event and in this case the goimports macro is configured to fire whenever the file is saved.

So consider this code:

Code: Select all

package main

import "bytes"

func main() {
    fmt.Printf("Hello world!\n")
}
When that code is saved it would be automatically fixed by goimports, reload into the editor and code would end up looking like this:

Code: Select all

package main

import "fmt"

func main() {
    fmt.Printf("Hello world!\n")
}
In addition since the compile the current file request always saves the file before running the compile the file save trigger will also fire, meaning you should never see a compiler import error again :)

Note: For details on setting up the Go compiler refer to this tutorial.

Setup for New Zeus Users
For user new to Zeus wanting to try out this feature just do the following:
  1. Download the latest Zeus release found here: http://www.zeusedit.com/download.html
  2. Open any Go file, make a small change to that file and save those changes.
  3. Follow the instructions in the resulting dialog.
Setup for Older Zeus Users
For Zeus users already running an older version of Zeus just follow the following instructions:
  1. Download and build goimports which is found here: https://github.com/bradfitz/goimports
  2. Copy the goimports.exe to the Zeus binary install folder
  3. Make new goimports script by copying the gofmt script from this link: http://www.zeusedit.com/zforum/viewtopic.php?t=6819
  4. Find this line in the new goimports script

    Code: Select all

        -- build up the final styling command line 
         local cmd = "gofmt.exe" .. " " ..  cmd_options  .. " \"" .. file_name .. "\""
  5. Change that line to read as follows

    Code: Select all

        -- build up the final styling command line 
         local cmd = "goimports.exe" .. " " ..  cmd_options  .. " \"" .. file_name .. "\""
  6. Configure the script as per the instructions from this topic: http://www.zeusedit.com/zforum/viewtopic.php?t=6819
Cheers Jussi
Post Reply