Golang · Uncategorized

Golang Primer

Golang is simple, efficient and has potential to revolutionize  backend processing. It is the optimal language for microservices  architecture.

Best Practices

  • Prior to all checkins please run gofmt -w yourfile.go (there are many plugins)
  • Packages are given lower case, single-word names; there should be no need for underscores or mixedCaps
  • Interface names and variables Convention in Go is to use MixedCaps or mixedCaps rather than underscores to write multiword names
  • Only functions / variables starting with a capital letter would be usable outside the package. All other internal functions start with lower case
  • Panics should as much as possible not cross package boundaries and if they do it should be made clear in the package documentation.
  • There is linter tool https://github.com/golang/lint which recommends “proper” syntax for your programs (cd myProject; golint ./… )
  • Other great tools:
    • errcheck
    • go vet
    • go test -race

Deployment

Manage Go package dependencies using godep https://github.com/tools/godep

Building for Linux on mac

Setup Cross compilation One needs to compile Go compiler for different target platforms and architectures. This is done from src folder in go installation. If you use Homebrew on OS X, then you have a simpler solution: $ brew install go –with-cc-all # All the cross-compilers Then to build: GOOS=linux GOARCH=386 CGO_ENABLED=0 go build -o appname-linux appname.go

Testing

Test tables

We can use anonymous structs to write very clear table tests that have multiple inputs and expected results without relying on any external package. E.g.

var testTable = []struct {
		isdev    bool
		expected string
	}{
		{true, "/Users/"},
		{false, "/home/httpd/"},
	}

	for _, test := range testTable {
		config.IsDev = test.isdev
		actual := getUsersHomeDir()
		if !strings.Contains(actual, test.expected) {
			t.Errorf("getUsersHomeDir: Expected %s, Got %s", test.expected, actual)
		}
	}

Gotchas

No built-in race condition safety

Go does not restrict access to shared data. A Go programmer has to explicitly synchronize and manages shared resources, else there could be race conditions making for some messy real-time problems.

Date Formatting

Go just doesn’t use an abstract syntax for datetime components (YYYY-MM-DD), but these exact numbers. No other number will do!

These are predefined layouts for use in Time.Format and Time.Parse. The reference time used in the layouts is the specific time: Mon Jan 2 15:04:05 MST 2006 which is Unix time 1136239445.

Since MST is GMT-0700, the reference time can be thought of as 01/02 03:04:05PM ’06 -0700 To define your own format, write down what the reference time would look like formatted your way; see the values of constants like ANSIC, StampMicro or Kitchen for examples.

The model is to demonstrate what the reference time looks like so that the Format and Parse methods can apply the same transformation to a general time value.

Maps and struct

Assign a struct field off map has to be done by first creating the element in golang.

type apiNode struct { method, expectedfile string }

var handlerMap map[string]apiNode handlerMap = make(map[string]apiNode)

handlerMap[APIName] = apiNode{"GET","a.json"}

Reference Links

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s