Ankeet's backend garden

go

  • it's a compiled language
  • use go run <filename.go> when trying to run the code as a script
  • use go build <filename.go> when you want to build a binary - so cool

no runtime needed for the package to be deployed, isn't it the best?

this makes sure you don't have to install something else for your code to run, the runtime or virtual machine is built in - the code will never be out of sync of the place it's deployed and will always be compatible - imagine using node lower version for es6 features - booom, or java 8 in jvm 6

  • go tools and 3rd party libs are managed differently than JavaScript, there's no central registry. Everything is done via git repositories
go install github.com/rakyll/hey@latest
  • what it does behind the scenes is fetch the code, build it and add it to $GOPATH/bin, since it's a binary nothing to install, just use it as a command

    • [doubts] how would it work inside the program?

formatting, linting

  • go comes with a formatter by default - go fmt <filename.go>
  • no prettier required :P
  • golint . works and finds lint mistakes
  • go vet <filename.go> finds unused vars etc

build

  • [go] uses [make] to build the projects

types

  • each variable which is defined but not initialized will have a value 0 unlike undefined in JavaScript
  • const can only be used for literals, we can't say in code that result of an operations of two variables is const
const x = 10.3
// valid as x is a literal
const y = math.Remainder(x, 2)

error: math.Remainder(x, 2) (value of type float64) is not constantcompilerInvalidConstInit

scope

  • go variables are block scope, vars declared outside the function at the package level are available throughout
  • do they shadow? [doubts]
  • use shorter variable names if the scope is short like a function but if the variable is added in the package scope then give full name as it should be clear what it's doing

arrays in Go

  • they are not that greatly used, whyyy?
    • since go is a strongly typed language and the type of the array contains the length of the array too
    • which makes it difficult to work with as we can't write code or functions which can operate on any array or perform operations
    • also length needs to be specified while creating an array; fixed length not variable

[slice]s

  • they are what arrays are in other languages
  • you can grow them decrease them and when creating you don't have to specify the sizes
package main
import (
"fmt"
)
func main() {
var x = []int{2, 3, 5}
x = append(x, 100, 200)
fmt.Println(len(x))
}

undefined in JavaScript but nil in Go

  • nil means there's no value in the slice
go