Why I Like Go

Advertisement

Advertisement

I think Go is a well designed and fun language to use. The standard library is pretty amazing, but here are a few things I like about Go. At the end are several references and links if you want to learn more about Go! Also check out my code snippets and references on GitHub github.com/NanoDano/reference

Syntax

var x int // Declare variable that initializes to 0 x = 10 // Set x to 10 i := 10 // Declare and initialize i to 10. Type is inferred by literal or function return type

Things are statically typed which gives you some fine control and no question about the data you are working with. You can declare without specifying the type which gives you the feel of the dynamic languages.

Concurrency

go myFunction()

When doing web work, if we have to make several requests, we don't want to wait sequentially for each request. This is where concurrency shines. Simply use the keyword "go" before calling a function, and it will spawn a goroutine to run. Multiple requests can be made concurrently and if one hangs or times out, it will not block the other goroutines.

Run like a script

go run myprog.go

With the go run tool it will compile on the fly and run without leaving a binary file behind. It compiles so fast you don't even notice it. When you are finally ready to distribute, use go build to compile a binary. It's easy to compile for x86 and x86_64 for Windows, Mac, and Linux. It also compiles to ARM. Cross compiling is easy and resources are linked at the bottom.

Auto formatting

go fmt mySource.go

No more arguing about format standards because go fmt will format all your code for you to fit the standards. It's nice because everyone's code should be formatted the same, making it easier to read because your eyes know exactly where to look for things, and can spot the patterns easily.

Auto documentation

go doc mySource.go

Much like the auto formatting, go doc provides auto generated documentation based on properly commented code.

Conclusion

I've had a lot of fun with Go so far. Check out my go snippets and references at https://www.github.com/NanoDano/reference, or my mirror http://git.devdungeon.com/go. The standard library comes with great network and http tools. Standard library lets you create a web server and even includes a template package! It makes writing a web app really simple. It covers a wide range of uses and I'm just getting started. I highly recommend checking it out.

References:

http://tour.golang.org/
https://code.google.com/p/go/downloads/list
https://www.youtube.com/user/gocoding
http://dave.cheney.net/2012/09/08/an-introduction-to-cross-compilation-with-go
https://github.com/davecheney/golang-crosscompile

Advertisement

Advertisement