Go Help

Go Format V2


At the end of the Go Format Chapter, I pointed out some of the limitations of  ‘gofmt’ with Gedit.  This is a much more complete script that does the following.

  1. If the reformat is successful, with no errors, it outputs the reformatted file, with a commented datetime stamp at the bottom, in the current edit tab.
  2. If the reformat produces errors, it restores the original source file in the current edit tab and displays the errors in either, a Pop Up Window, a Pop Up Terminal or a new Gedit tab.

It also handles file and directory names with spaces (not a recommended practice).


  1. Make sure that you have Zenity installed.  See the end of the Plugins Chapter.
  2. If you have a general ‘temp’ directory, set it as described in the ‘Gedit – An IDE for Go’ chapter.
  3. Open Gedit and Go to Tools>Manage External Tools.
  4. Choose Go Format, or create a new tool.
  5. Copy and Paste the script below into the code panel, overwriting the existing code.
  6. Make the other settings as documented in ‘Tool Settings’ in the script below.
  7. Set the OUTPUT switch below ‘Tool Settings’ in the script below.


# Go Format - Executes 'gofmt' against the Go source file
#               in the current Gedit tab
#    Manual - www.gohelp.wordpress.com - 'Gedit - An IDE for Go'
#   1.  If 'gofmt' has no errors,
#           the formatted output is put in the Current Edit Tab.
#   2.  If "gofmt" has errors,
#           the original file is put in the Current Edit Tab and the
#           error listing can either go to a New Tab or a Popup Window
#   *Tool Settings: Save = Current Document, Input = Nothing,
#                   OutPut = Replace Current Document
#OUTPUT=1   errors to New Tab
#OUTPUT=2   errors to Popup Terminal
#OUTPUT=3   errors to Popup Window
OUTPUT=3  # Set OUTPUT Switch

DOCPATH="${GEDIT_CURRENT_DOCUMENT_PATH//%20/ }" # ex tool puts %20 for spaces

if   ! ( test -z "$MyTemp" ) # if MyTemp exists in environment
    TMPDIR="${DIR}"  # else set TMPDIR to current document directory

gofmt "${DOCPATH}" 1> "${TMPDIR}"/GoFmt.tmp 2> "${TMPDIR}"/GoFmtErr.tmp

if   (test -s "${TMPDIR}"/GoFmt.tmp) # TRUE if GoFmt.tmp exists and is not empty
    cat "${TMPDIR}"/GoFmt.tmp       # Show reformatted source in current tab
    echo "//   Reformatted by  "  $USERNAME "   "$(date) # Datetime stamp

elif (test -s "${TMPDIR}"/GoFmtErr.tmp) # TRUE if GoFmtErr.tmp exists
    cat "${DOCPATH}"          # Show original file in current edit tab

    case $OUTPUT in
        1) gedit "${TMPDIR}"/GoFmtErr.tmp &
        2) TermCmd="less \"${TMPDIR}\""/"GoFmtErr.tmp"
           gnome-terminal --command "${TermCmd}" -t "Go Format ${TermCmd}" &
        3) nLines=`wc -l "${TMPDIR}"/GoFmtErr.tmp|awk '{print $1}'`
           if [ "${nLines}" -gt 28 ]
           zHeight=$(( nLines * 25 )) # Set width of Zenity window

           longLine=`wc -L "${TMPDIR}"/GoFmtErr.tmp|awk '{print $1}'`
           if [ "${longLine}" -gt 80 ]
           zWidth=$(( longLine * 9 )) # Set height of Zenity window

           zenity --text-info \
               --height=${zHeight} \
               --width=${zWidth} \
               --title="Go Format" \
               --filename="${TMPDIR}"/GoFmtErr.tmp \ &
        *) echo "Please Set Go Format OUTPUT option" > "${TMPDIR}"/GoFmtErr.tmp
           gedit "${TMPDIR}"/GoFmtErr.tmp &
	cat "${DOCPATH}"     # Show original file in current edit tab

sleep 1     # make sure new shell tasks have displayed, then exit
rm "${TMPDIR}"/GoFmt.tmp
rm "${TMPDIR}"/GoFmtErr.tmp


  1. Open Gedit and Go to Tools>External Tools>GoFormat.
  2. You should see output as described in the Introduction.

Blog at WordPress.com.

%d bloggers like this: