I started learning Golang recently and after completing a project I wanted to share some of the first impressions of transitioning from Javascript/Node.js to Go.

  1. Types. Types are built-in in Go. This is way cooler than Typescript because there’s no extra setup effort, all Go libraries are typed so absolutely all of the ecosystem has types. This also means that there’s no need for tools like Webpack and build step is so much simpler!
  2. Go modules are the equivalent to NPM packages. However, Go’s package manager is very basic, for example, you can’t execute scripts from it. In my project I had to use make to launch scripts.
  3. There’re no truthy or false values in Go (similarly to Java). The following code will not compile:
test := 5
if test {
    // ... do something
}
  1. Structs in Go are values, like in C. This means that after passing a struct as function parameter, changing the struct inside that function and returning from the function the struct will not retain the changes:
package main

import (
	"fmt"
)

type test struct {
	number int
}

func change(num test) test {
	num.number = 7
	return num
}

func main() {
	num := test{ 5 }
	change(num)
	fmt.Println(num.number) // prints 5
}

This is because function parameters are copied by value and structs are values in Go. In case you want to mutate a struct from inside another function it should be passed as a pointer. Passing struct as a pointer is also preferred if the struct is big so that all of its fields are not copied.

#go #javascript 

Javascript to Golang: First Impressions
2.60 GEEK