Tuesday, March 14, 2017

Getting Started with Golang Part 2: Overview of Utilities

Golang, like most other programming languages, parses the code into an abstract syntax tree. Golang has a lot of utilities that use this AST to help with coding.
go fmt
This built in command parses golang source code into AST and translates it back into code with easily readable spacing/tabbing. In nano, it is keymapped as Control-t, and there are plugins for VIM and EMACS.
It is also accessible in the standard library as "go/format"
go doc
Like Java and many other languages, Go has an automatic documentation tool (godoc). Godoc is also accessible as a website that will automatically generate documentation given a package address (e.g. github.com/Shopify/sarama).
go get
Golang uses addresses for importing packages (basically take the URL and remove the protocol prefix). (Note: standard libraries don't follow this pattern). For example, to import github.com/Shopify/sarama, you would import it as:

import (
      "github.com/Shopify/sarama"
      //other imports
)

(note: Blogger wont let me tab, so I tried to space)
The command go get clones the library with git into your Go path so that you can use it.
GopherJS
This is not an official utility, but I included this because this is incredibly useful. This translates Go packages into extremely fast JavaScript - allowing it to be run in a browser at high speed:
(above: HTML5 game benchmark with GopherJS with >10k objects running at 41 FPS)
GopherJS also has a command (gopherjs serve) which opens a web server and dynamically translates installed go packages to JavaScript. GopherJS itself can be converted to JavaScript and run in a browser, hence the GopherJS Playground.

No comments:

Post a Comment