Go Help

Go Build

Introduction

Even though our modern GUI desktops make moving between windows easy, I still like to edit, compile, run from within my IDE with a quick keystroke or mouse click.  As shown in the Chapter Formatting, Gedit has the ability to run external commands and therefor to do many things.  NOTE:  With gEdit 2.30.4, you can now go to the error line in your code.  Just click on the error in the botton pane.

Step 1

  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.  The first item is Build.  Click on it and you will see, in the right hand pane, a basic script to call a traditional make file.  If this suits your needs, then use it as is, modify it or substitute your own.  Below shows the basic procedure.

I wanted to follow the 6g, 6l procedure that I was using from the command line and that is shown in the Go tutorial.  I also wanted a build script that I could run from within Gedit or from a terminal, so I put all of the code in MakeGo, which we will see later.

  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 Build’.
  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,  type  “./MakeGo  $GEDIT_CURRENT_DOCUMENT_NAME”.  This executes MakeGo in the same directory as the source file you are editing.  Notice $GEDIT_CURRENT_DOCUMENT_NAME.  Gedit creates useful environment variables you can use.  Here is some documentation about External Tools:  http://live.gnome.org/Gedit/ToolLauncherPlugin

Below the code panel you can set:

Shortcut Key

Just press the key combination you want to use for Go Build.  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 the build.  This is necessary so the MakeGo script has the current file to compile.

Input

Set this to nothing.  $GEDIT_CURRENT_DOCUMENT_NAME passes the file name in this script.

Output

Set this to display in bottom pane.  This directs the output of MakeGo back to the bottom pane in Gedit.

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


Step 2

Here is my script for MakeGo.  Copy and paste this into Gedit and save as “MakeGo”  in your project source directory.

#!/bin/bash

# MakeGo

# Run Compiler and Linker for 6g, 6l
# In 32 bit environment replace 6's with 8's
#     could test $GOARCH to make this automatic

# To force environment variables when using non-interactive shell
#    recommend setting environment variables in $HOME/.profile
#export GOROOT=$HOME/go
#export GOARCH=amd64
#export GOOS=linux

if [ -n "${1}" ]    # Test for command-line argument present
then
    GoSourceFileName="${1}"
    GoBaseFileName="${GoSourceFileName%%.go}"  # Strips '.go'
else
    echo 'Please specify file name for MakeGo'
    exit 1
fi

echo "Compile     $GoSourceFileName"
if  ! (6g  "${GoSourceFileName}")  # Test for compile failure
then
    exit 1     # No need for 'Compile Failed', outputs errors
fi

echo "Link        ${GoBaseFileName}.6"
if (6l -o "${GoBaseFileName}.out" "${GoBaseFileName}.6") # Link success?
then
    rm "${GoBaseFileName}.6" # Link successful, remove intermediate file
    echo "Executable  ${GoBaseFileName}.out"
else
    echo "Link Failed"
    exit 2
fi

exit 0

I have 64 bit Linux.  If you have 32 bit, using 8g and 8l for compile and link, edit this file to change the 6’s to 8’s.  Also, if you have a problem with environment variables you can uncomment GOARCH, GOROOT and GOOS and set to the correct values for you computer.

Next, make MakeGo executable!  Open your File Browser, go to your source directory where you saved MakeGo, right click MakeGo and choose Properties.  Now choose the Permissions Tab and check the “allow executing file as program” box.  Or you can open a terminal, navigate to your source directory and type “chmod 777 MakeGo”, to make it read, write and executable for everybody (or whatever level of permission you wish).

TEST

Open Gedit and enter your “hello world” program,  save it with the “.go” suffix.  Press your Go Build shortcut key (Control 7 or whatever you assigned), or go to the menu and go to Tools>External Tools>Go Build.  If all is well you will see the MakeGo script messages or compiler error  messages in the bottom pane of Gedit.  NOTE:  You can now go to the error line in your code.  Just click on the error in the botton pane.

You can also run this MakeGo script from the terminal.  Change to your source directory and type “./MakeGo source file name” .

If you have a problem.  Is MakeGo in your source directory?  Is it executable?  Are your GOROOT, GOARCH and GOOS environment variables set to the correct values?  If you are 32 bit, did you change 6’s to 8’s in the MakeGo file?

Create a free website or blog at WordPress.com.

%d bloggers like this: