Recently I've decided to take a look at golang. I didn't know where to start because I didn't have any problem to solve. So I briefly outlined the first steps to cover. I want to create a simple web service or a couple of services for some SPA application. But first I need to get more familiar with golang environment and tools.
Installation
I opened the golang home page and quickly viewed its content.
It happened that my laptop has funtoo linux installed. I quickly typed in the console the following to install the golang package:
$ emerge -av dev-lang/go
I wasn't quite sure funtoo has golang in its repository. Fortunately there was golang version 1.4.2 which is the most recent stable. There is a lot of files installed but I was interested in binaries first:
$ equery f dev-lang/go | grep /usr/bin/
/usr/bin/go
/usr/bin/gofmt
Not much but lets run them and see its output:
$ go
The usage section briefly explains the commands available. I noticed few commands that do not belong to a compiler usually: clean, run, test, and get. Looks like it's a compiler, a runtime, a package manager and a test runner altogether which is not following the UNIX-way. I decided to figure out it later.
Lets see what is gofmt:
$ gofmt
...which outputs nothing but just waiting for input. Running with -h gives a bit more info. As far as I see it is a some kind of a linter for golang sources. I wonder why it is in a separate executable and not included into the go. Leaving it for now.
Write some code
There is nothing special about it just create a .go file and paste the example from golang site. I skipped the formatting for a purpose:
$ vim /tmp/hello_world.go
Now lets get back to gofmt and see how it works, the -w option overwrites file's content instead of writing formatted code to stdout:
$ gofmt -w hello_world.go
Works as expected but I noticed that it does not add a new line to the end of file:
By the way if you use vim you can add a hook for formatting with gofmt after buffer write:
:set autoread | autocmd BufWritePost *.go :silent !gofmt -w %
Build and run it
I've seen earlier from the go executable output that I can run the source file right away by using run command option:
$ go run hello_world.go
Hello, World!
Pretty straightforward but I noticed a small delay before program output probably caused by intermediate building or interpreting step of the run command. Also there was no binary file created.
Running with the build command option finally produced a binary:
$ go build hello_world.go
The size of the binary is about 2M:
$ ll hello_world
-rwxr-xr-x 1 user users 1.9M Jun 9 13:29 ./hello_world
Also it's not linked dynamically to anything:
$ ldd hello_world
not a dynamic executable
and is a 64-bit executable:
$ file hello_world
hello_world: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), statically linked, not stripped
Summary
As a conclusion I think golang tools are friendly and simple to start with. Except a few things I noticed. These are camel notation for functions and a single executable for building, testing, running and managing packages for your golang programs.
If you like you could try golang online without having it installed locally.