Thursday, February 2, 2017

Go - Environment Setup

Try it Option Online

You really do not need to set up your own environment to start learning Go programming language. Reason is very simple, we already have set up Go Programming environment online, so that you can compile and execute all the available examples online at the same time when you are doing your theory work.
This gives you confidence in what you are reading and to check the result with different options. Feel free to modify any example and execute it online.
Try following example using Try it option available at the top right corner of the below sample code box:
package main

import "fmt"

func main() {
   fmt.Println("Hello, World!")
}
For most of the examples given in this tutorial, you will find Try it option, so just make use of it and enjoy your learning.

Local Environment Setup

If you are still willing to set up your environment for Go programming language, you need the following two softwares available on your computer, (a) Text Editor and (b) The Go Compiler.

Text Editor

This will be used to type your program. Examples of few editors include Windows Notepad, OS Edit command, Brief, Epsilon, EMACS, and vim or vi.
Name and version of text editor can vary on different operating systems. For example, Notepad will be used on Windows, and vim or vi can be used on windows as well as Linux or UNIX.
The files you create with your editor are called source files and contain program source code. The source files for Go programs are typically named with the extension ".go".
Before starting your programming, make sure you have one text editor in place and you have enough experience to write a computer program, save it in a file, compile it and finally execute it.

The Go Compiler

The source code written in source file is the human readable source for your program. It needs to be "compiled", to turn into machine language so that your CPU can actually execute the program as per instructions given.
This Go programming language compiler will be used to compile your source code into final executable program. I assume you have basic knowledge about a programming language compiler.
Go distribution comes as a binary installable for FreeBSD (release 8 and above), Linux, Mac OS X (Snow Leopard and above), and Windows operating systems with the 32-bit (386) and 64-bit (amd64) x86 processor architectures.
Following section guides you on how to install Go binary distribution on various OS.

Download Go archive

Download latest version of Go installable archive file from Go Downloads. At the time of writing this tutorial, I downloaded go1.4.windows-amd64.msi and copied it into C:\>go folder.
OSArchive name
Windowsgo1.4.windows-amd64.msi
Linuxgo1.4.linux-amd64.tar.gz
Macgo1.4.darwin-amd64-osx10.8.pkg
FreeBSDgo1.4.freebsd-amd64.tar.gz

Installation on UNIX/Linux/Mac OS X, and FreeBSD

Extract the download archive into /usr/local, creating a Go tree in /usr/local/go. For example:
tar -C /usr/local -xzf go1.4.linux-amd64.tar.gz
Add /usr/local/go/bin to the PATH environment variable.
OSOutput
Linuxexport PATH=$PATH:/usr/local/go/bin
Macexport PATH=$PATH:/usr/local/go/bin
FreeBSDexport PATH=$PATH:/usr/local/go/bin

Installation on Windows

Use the MSI file and follow the prompts to install the Go tools. By default, the installer uses the Go distribution in c:\Go. The installer should set the c:\Go\bin directory in window's PATH environment variable. Restart any open command prompts for the change to take effect.

Verify installation

Create a go file named test.go in C:\>Go_WorkSpace.
File: test.go
package main

import "fmt"

func main() {
   fmt.Println("Hello, World!")
}
Now run the test.go to see the result:
C:\Go_WorkSpace>go run test.go
Verify the Output
Hello, World!

No comments:

Post a Comment