Go (Golang)

Getting Started

Important Stuff to Note

  • Go does not have classes.

  • In general, all methods on a given type should have either value or pointer receivers, but not a mixture of both.

  • A type implements an interface by implementing its methods. There is no explicit declaration of intent, no "implements" keyword. https://go.dev/tour/methods/10

  • Goroutine is more or less similar to Java's process fork. The goroutine will also be stopped if the main process is stopped.

    • Communications can be done between goroutines with channel.

Method with Pointer Receiver

A method with pointer receiver behaves differently:

func (v *Vertex) Scale(f float64) {
    ...
}

The vertex's value will change on the struct. This is similar to having func Scale(v *Vertex, f float64) which will use the same Vertex instead of copying the Vertex.

Closure

It might not be a good idea to use closures a lot, this is because closures do have a side-effect.

It is still okay to use it sparingly, one of the use-case where I might use it is for memoization:

Struct Embedding

If a struct's field do not have a name, it is a struct embedding.

Last updated