Starting Golang programming- Setting up Go environment
Contents:
0x00 -
Golang0x01 -
Software0x02 -
Setting up Environment variables0x03 -
Starting Golang programming0x00 - GolangGolang - The Go programming language is an open source project to make programmers more productive. Go is expressive, concise, clean, and efficient. Its concurrency mechanisms make it easy to write programs that get the most out of multi core and networked machines, while its novel type system enables flexible and modular program construction. Go compiles quickly to machine code yet has the convenience of garbage collection and the power of run-time reflection. It’s a fast, statically typed, compiled language that feels like a dynamically typed, interpreted language. Go can be used for alot of scenarios; as a system programming language, for web publishing, on the android platform and many more uses a mature programming language is expected to have.
DocumentationThere is a substatial amount of documentation written about Go.
The Go Tutorial,
Effective Go Document and
http://golang.org/doc/ are a very good place to start reading up on Go. Starting from Go v1, Go comes with its own documentation and can be viewed using:
go doc builtin
As you go on, you will realise alot of resembrance between Golang and Erlang. Notable differences between Erlang and Go is that Erlang borders on being a functional language, where Go is an imperative one. And Erlang runs in a virtual machine, while Go is compiled. Go also has a much more Unix-like feel to it.
0x01 - SoftwareIn order to compile any golang source code, you are gonna need the go toolchain installed on the system. After compiling the code should run on w/o the go tools present on the system, or on any system if cross-compiling is used.
Windows:Ok, yeah-yah. I will start with you b'se as always, everything is simplified for you. Point your browser to
http://golang.org/dl/ and get the latest installer. Click, click and done. You have a go environment. Afew things might not work yet b'se some few environment variables ain't setup yet. For windows environment variable, check out
http://golang.org/doc/install#windows for details.
OSX:Here the easiest way is to use homebrew throurh your Terminal.app:
$brew install go --cross-compile-common
All should be done except for afew things like setting up environment variables and VCS setup.
Linux:Well a simple sudo apt-get install go , sudo yum install go or any specific package manager install command of you system should do it. Be happy i saved you the installing from source part though i know Gentoo people will still do it.
0x02 - Setting up Environment variables
By convention, all your Go code and the code you will import, will live inside a workspace. This convention might seem rigid at first, but it quickly becomes clear that such a convention (like most Go conventions) makes our life much easier. So lets tell GO where our workspace is. The commands should be the same on most systems except for windows users who should substitute 'export' with 'set':
Let's create a folder named "go" and make this our workspace.
$mkdir $HOME/go
$export GOPATH=$HOME/go
Problem is that on the next system restart, Go won't know where our workspace is. So lets put this in our profile. You can use your bashrc or bash profile.
$vi $HOME/.bash_profile
or
$vi ~/.bashrc
You can always use your favorite editor other than vim. Now add these lines to your profile:
#export GOROOT=$HOME/go
export GOPATH=$HOME/go
export GOBIN=$GOPATH/bin
export PATH=$PATH:$GOBIN
export GOOS=linux
It is always a good practice not to set your GOROOT the same as your GOPATH, that is why i commented out GOROOT [Don't remember where i read that].
Setting up Version Control System tools:
If you are an avid developer, most of these should already be on your system but it doesn't hurt to check. Lucky, they should be in your package manager already and windows users can download their binaries and install them.
sudo apt-get install mercurial hg git bzr
or
sudo yum install mercurial hg bzr git
With these setup, it is very easy to get third-party libraries with go. You should now use your new tools to install afew extra tools for go:
$ go get code.google.com/p/go.tools/cmd/godoc
$ go get github.com/golang/lint/golint
Enough with this boring stuff, lets get some code going.
0x03 - Start Go programming: Get your feet wet."leave your object oriented brain at home. Embrace the interface." - @mikege-
hardGo programming is fairly easy, create a .go file, put code into it, compile and lose the code. You are good to Go.
Our generic Hello_world.go
package main
import "fmt" // Implements formatted I/O.
/* Print something */
func main() {
fmt.Printf("Hello, world")
}
Now execute this to run a copy without an executable in $GOBIN:
go run hello_world.go
or
$go install hello_world.go
then
$GOBIN/hello_world
I would advise you to grab a Golang book in the ebook section or pick from alot of free books the Go community provides on the webs. Otherwise the Go documentation is enough on its own coupled with the Go
tour