Getting started with Google's Go
Written by Mike James   
Friday, 04 June 2010

Want to give the new Google Go language a try? Then follow our simple guide to getting started using Windows.

For a more up-to-date introduction to Go see the three part series that starts with A Programmer's Guide To Go With LiteIDE.

 

Go is a new language (see Go with Google - Yet Another Language!) and it isn't developed enough at the moment to have easy-to-use IDEs and lots of support. If you would like to give it a try then you can download either the Linux or the Windows version. There are lots of tutorials on getting started with Linux Go but not many on using the Windows version. As the Windows version is a binary and hence fairly easy to install it has the advantage of being easier to get started with - once you know how. So the first part of this tutorial is all about setting up Go under Windows. Then we move on to look at some of the basics of the language.

Windows Go

The first thing to do is download the latest ZIP file containing all of the binaries, some tools and sample programs. The main Go website is at http://golang.org/ but the Windows port is hosted at http://code.google.com/p/gomingw/. There you will find the latest ZIP file in the Downloads section and it will be called something like gowin32. You should download the latest unless there are any dire warnings of possible bugs. 

Down

The download list

Once downloaded you should unzip the entire ZIP into a suitable directory - your personal documents directory will work perfectly well.

For the rest of this article it is assumed that you have a directory called Go in your documents directory. That is

C:\Users\yourname\Documents\GO\

where yourname is replace by your actual user name, contains all of the files and directories in the ZIP and in particular a bin directory and a test directory.

Our next job is to set up the environment variables needed for it to all work properly. You can set these at the global level but if you are just trying Go out then it is probably better to set up a temporary working environment rather than clutter you machine up.

To do this start a command prompt and enter the following commands:

set GOROOT=C:\Users\yourname\
Documents\Go\
set GOBIN=%GOROOT%Bin
path %GOBIN%
set GOARCH=386
set GOOS=windows

GOOS had to be set to mingw in versions of Go earlier than May 2010.

You can check that everything has been set correctly by simply typing set and examining the list of environment variables. If you know about batch files then simply store these commands in a batch file in the Go directory and you can run them each time you start a new programming session.

Hello World

To check that you have things roughly correct you can now try compiling a test program - helloworld.go.

The program is stored in documents\go\test and we might as well make things easier by making this our current directory. Enter

cd documents\go\test

now we can start the compiler on the source file helloworld.go. There are a range of different compilers supplied but for Windows the one that you want to use is 8g - the 8 comes from the eight in 386. Use the command

8g helloworld.go

notice that you do need the extension .go for this to work.

The compiler produces an intermediate code file called helloworld.8 and this has to be linked with other intermediate code files to create a final binary executable file. The linker is called 8l and the command you need is:

8l -ohelloworld.exe helloworld.8

The -ohelloworld.exe defines the name of the output file and the helloworld.8 is the input file. In a more complicated project there might be a number of intermediate code files that have to be linked together.

Now, at last you can run the program. If you enter

helloworld

then you should see

hello,world

printed as a response.

If so you have completed a compile, link, execute cycle under Go and all you have to add to it is an Edit step to start developing your own programs.

Your own Hello World

To take the test one stage further we can create our own Go program using Notepad. Start Notepad and enter:

package main

import "fmt"
func main() {
fmt.Printf("Hello World\n")
}

 

Save this as MyHello.go in a new directory MyProjects. If you surround the file name in double quotes then Notepad wont add .txt onto the end - if it does simply rename the file. Also make sure you save the file in UTF8 format.

Next change directory to MyProjects and enter the following commands:

8g myhello.go
8l -omyhello.exe myhello.8
myhello

and you should see "Hello World" displayed.

From here it is just a matter of creating your own programs in Go.

Try the tutorial to get started with some examples.

 

For a more up-to-date introduction to Go see the three part series that starts with A Programmer's Guide To Go With LiteIDE.

<ASIN:0763776270>

<ASIN:1590591348>

<ASIN:0805316701>

<ASIN:0471113530>

<ASIN:155860698X>

Last Updated ( Tuesday, 24 December 2013 )
 
 

   
RSS feed of all content
I Programmer - full contents
Copyright © 2014 i-programmer.info. All Rights Reserved.
Joomla! is Free Software released under the GNU/GPL License.