DEV Community

Frank Li
Frank Li

Posted on

A few Golang notes

I've taken a liking for Golang's no-frills and elegant approach to programming. Compared to C, Golang takes care of a lot of the intricacies and annoyances of pointers and manual memory allocation while still retaining high efficiency.

Another attribute of Golang that contributes to its appeal is the presence of built-in, lightweight concurrency with goroutines and channels.

However, after using Golang for a while I've discovered (or rediscovered) some interesting notes:

Golang doesn't require explicit dereferencing to access struct attributes

type p struct {
    u string
}

val := p{"hello"}
point := &p{"world"}

fmt.Println(val.u)
fmt.Println(point.u) //no dereferencing required

Unlike C, Golang's compiler is smart enough to access the attributes of a pointer without the need for explicit dereferencing.

Golang has no generic type

Unlike many popular languages, Golang doesn't have a generic type. Instead, Go programmers use an empty interface

func(interface{}){
    //code here
}

to write reusable or type-agnostic code.

Golang's proprietary gobs are based on Google's own protocol buffers

Interestingly, Go uses its own gob encoding as its standard encoding format (although standard JSON, XML, and YAML encoding libraries are including and just as usable. With the popularity of some of the other formats (including Google's own protocol buffers), why would they do this?

The reason given in a Go Blog post is that gobs are self-describing binary encodings that, while Go-specific, provide efficient data transmission over networks.

Top comments (0)