View all posts →

Notes about Golang

Why GO? Personally i like it a lot, i have tried the very basics about Go, i really liked it, its great for terminal apps, be, infrastructure etcc

Basics of Go

Go is a statically and Strongly typed language and also its a compiled language unlike JS, Python(dynamically typed). It also supports concurrency model, It also supports type system out of the box(like typescript for javascript). Since it is compiled language it turns our code into machine code quickly which makes the application faster on the run time. It also has built in garbage collection. Example for Statically and strongly typed language const str: string; const num: number; console.log(str + ":" + num) => this will work in typescript(only statically typed) var str string; var num int; println(str + ":" + num) => will not work in go(since it is both statically and strong type)

Unlike python, javascript etc Go is a compiled language, meaning it will compile the human readable code and converts into machine code where that output machine code alone can be transfered to other people or put in server's to execute. Unlike hpython or js, other people or servers dont need js runtime(node) or python runtime, since in go we are transferring the direct machine code the OS(platform specific) can able to execute directly.

Memory Management in GO

Python, javascript etc are managed by garbage collector(Automatic memory management), and the code(Garbage collector code) that needs to mange this memory will be in the python or js runtime. But languages like C, C++ these are manual memory management, and needs to handled manually in our program. Go is also managed by garbage collector(Automatic memory management). Now there arise a question, since go is a compiled language like C, C++ etc, where does the Garabge collector code will be present The answer is In every Go program when we are compiling, the garbage collector code(Go Runtime) will also be compiled along with it.

Packages, Modules, go mod init and imports

Packages

Go deals with packages and modules for their code structure.
Package => this is the smallest code unit, Go expects a file with package main with func main in it. This is by default non changeble, in ur folder there needs to atleast one package main with func main, otherwise go doesn't know which file to run it/basically start with it. consider this as node index.js(where index.js is typically JS's root file/starting file to run).
Also all .go files inside a single folder should belongs to the same package.

hello-world

    -> main.go(filename doesn't need to be main, it could mani.go, but when u run u need to give the proper file name go run main.go(mani.go))

    -> util.go

    -> helper.go

If u see the above structure, there are 3files inside hello-world folder, all these should have the same package name, and atleast one file should have main.go function.
It doesn't needs to specifically package main, it could be package hello, package temp etc, anything, but when u try to run ur code for eg: go run main.go in this, compiler will search for main func and package main, if not it will not run anything.

main.go


package test

import "fmt"

func main() {

  fmt.Println("Hello")

}

when u try to run the above program go run main.go, you will getting the error as ...is not a main package.

Then why package exists, packages are individual snippets that can do their own job, consider package as libraries(npm libraries in node).you can use/import any particular packages to other codes.

hello-world

    -> main.go // package main

    -> util/

        -> util.go // package util

    -> helper/

        -> helper.go // package util

From the above example it is clear that, packages requires folder, each folder is kinda each packages. In the above example main.go can import util.go and helper.go using import ("hello-world/util").
If u have a package main in main.go and importing util/ into main.go, you cannot have package main and func main in util.go.(Go Compiler expects only one package main and func main across ur program).

Final note on pacakges

Packages are smallest unit of program, packages are similar to how js import different files.

In js each file is importable, but in go those are separated to folders and packages.

Only one file can have package main and func main().

Packages are also serves as libraries, you can import any packages like that.

Modules

Modules is basically a project or a collection of packages or an application.
One module can have multiple packages inside with different folder. To create a module, you need to use a command go mod init {MODULE_NAME}
Mostly MODULE_NAME is the folder name, but its not a defined rule.

Imports

-> Once u define the modules, now its easy to import and use it anywhere.
-> Consider u have a module called hello-world and pacakge in it called util/util.go(pacakge util).
-> You can import the above package anywhere in the world inside go code using {HOST_URL(typically github)}/hello-world/util
-> This what i meant pacakges can be libraries, in the above hello-world is module and util is pacakge.
-> If u want to use util inside hello-world module itself u simply needs to import by import "hello-world/util".
-> YOu can also import standard libraries from Go like "fmt"(similar to console.log), "net"(similar to node http) etc

NOTE: In go pacakge main file will only does import, nothing can be exported from pacakge main file.

In Go there is no export keyword, function name/variable name/structs/types that starts with capital letter
by default mean they are stuffs for export, lowercase names by default are internal to that file.

Variables

As previously stated Go is a statically typed language, so u need to specify ur type on every variable declaration

import ( "fmt" )


func main {

  // 1 way
  var name string
  name = "Manikandan"
  var age int
  age = 29

  // another way
  name := "Manikandan"
  age := 29

}

In the above if u can see, u can create a variable using the type in 1way, another way the compiler itself will inference the type based on the value.

Unlike javascript, where the default value of any variable if its not initialized would be undefined, in go, the default value is based on the type
. -> for int - 0
-> for float - 0.0000
-> for string - ""
-> for bool - false

Constants

Constants are something similar to every other programming language in go, you can assign a constant something like
const MAX_NUM = 10.

The only difference is, in go whatever u left unused the compiler will throw error go won't let u compile or run. but const can be left unused, u won't face any issues from the compiler.

The reason for above, is of course memory management, for normal variables, compiler doesn't know how much memory need to allocate for any particular variable, coz it can increase or decrease, i mean var name string; name = "manikandan"; this can be later changed to name = "mani" or "ma" or "Manikandan Arjunan" etc, but constants are predefined value, so compiler does know the exact amount of bytes that it need to allocate.

View all posts →