Joseph Mullins

Programmer | Security | Automation

joseph@ropeney.com

Trying out GoLang

Back

GoLang is looking really promising. It's super ease of portability and almost rapid development ability is positioning itself strong in the market. I have drawn a strong interest to it for it's ability to quickly write network/operations tools, compile them on any device and send it out so easily. I'm not in a position to judge performance as I'm very new to the language, though my opinion is it's probably fast enough.

There are plenty of ways to learn GoLang so I'm not going to even try cover that here. Even though the language specification is extremely small and you can get up to speed rather quickly with a short tutorial. Instead I'm going to focus on the things, as a new person to GoLang, that I have liked the most.

Tooling

Go Get

go get https://github.com/golang/example will go download the GoLang package from Github and put it into your GOPATH.
import "github.com/golang/example" will then import the package to your project.

Go Format

gofmt -w *.go will format your projects files, in-place, to the GoLang standard formatting. I suggest setting this up to do it when you save with your choice of editor. For VIM users, I highly recommend faith/vim-go.

Standard Library

The language is so simple, but has such a powerful exhaustive standard library that includes so many powerful tools. Check out how easy it is to make a Basic Web Server

Variables

Variable assignment can automatically infer type or you can explicitly define. This gives you almost the luxury of a Dynamic language with the power of a Static language.

var height int = 186 // Explicit Assignment
height := 100 // Short assignment, only use in local scoped variables.

Unused Variables

Unused variables results in a compiler error, this helps keep code very clean and concise when refactoring and working in large code bases.

Strings

Strings can be concatenated with the + operator. A small... lovely... luxury.

Shorthand increment/decrement

i:= 0
i++

Looking at you Ruby.

Range through collections

numbers := []int {1,2,3,4,5}
for _, number := range numbers {
  fmt.Println(number)
}

This gives a really quick and nice way for iterating through collections and just to me looks very concise and neat. The _ idiom for discarding the index, to me can sometimes be annoying.

Functions

Returning multiple variables

age, height := getDetails()
func getDetails() (int, int) {
  return 26, 186
}

Multiple Arguments in a function

func collectionOfInts(args ... int) {
  for _, number {
    ...
  }
}

GoRoutines

Goroutines are very lightweight Threads, that aren't really threads. They require alot less memory then threads in other languages. The idea is that GoLang maintains a pool of OS thread's in the runtime, allocating Goroutines to them so the OS is not aware of what your program is doing. This allows quick and cheap switching, easily ignore blocking calls from locking a Thread; without the cost of them.

While you will need to consider syncing variables as they are in the same memory space, creating a go routine is extremely simple; prefix a function with go

func main() {
 go printStuff() // new goroutine
 printStuff() // in main goroutine
}

func printStuff() {
  ... // Print things
}

Clojures and GoRoutines!

go func(number int) {
  for i := 0; i < number; i++ {
    fmt.Println(i)
  }
}(6)

Scoping

GoLang does not use public/private keywords for visibility identifiers. If the first letter is a capital, it is public, lower case is private. This applies for variables, methods constants etc.

struct Person {
  age    int // private
  Height int // public
} // Public Struct in package.

 

Cross Compiling

I develop on two main machines, a Macbook and a Linux desktop. A compiled language has always been a concern about ease of use between different platforms. While cross compilation isn't a new thing, I haven't experienced anything as easy as the way Go 1.5+ does it.

This gives me a quick build for my Raspberry Pi

GOOS=linux GOARCH=arm GOARM=7 go build

This gives me a quick build for my Windows friend

GOOS=windows go build

Or a quick file to run on any of my Linux servers

GOOS=linux go build

Comments