Introduction To Golang
Introduction To Golang
Go is a statically typed compiled language, often known as C for the 21st century.
Popular choice for server side applications.
The source code is compiled down to the machine code which means it will generally
outperform interpreted languages.
It is also known for its fast compile times.
Due to these restrictions, compilers can do more thorough checking of the code
before compiling, leading to fewer bugs.
3. Compiled Language
Go comes with a compiler which translates your code to the machine level code.
It does this by producing binary files which can be run as standalone programs.
This can be relatively faster than the interpreted languages such as python.
Interpreted languages which use interpreters which translate the code line by line
adding more overhead which can make the execution relatively slower.
5. Simplicity
Cross-Platform
Cross-platform means that software can run on multiple operating systems (OS) without
needing modification.
In the context of programming languages, this means you can write code once and
compile it to run on different platforms like Windows, macOS, and Linux.
Unlike Java's bytecode, which runs on any JVM, Go binaries are platform-specific.
This means a binary compiled for Windows won't run on Linux or macOS, and vice
versa.
The Stack
A stack can have stack frames where each stack frame is dedicated to a function
Once the function is done executing, the stack frame associated with that function is
marked as invalid.
Golang doesn’t clean after itself in the stack.
The stackframe still exists, but it is somehow not used by the go.
As the new line of the code is executed (println), a new stack frame is created dedicated
to the function and then it is executed.
Returning Pointers
At the end of the day, only the compiler knows whether the variable will be on stack or
on heap.
Whenever it can, the Go compilers will allocate variables that are local to a function in
that function’s stack frame.
However, if the compiler cannot prove that the variable is not referenced after the
function returns, then the compiler must allocate the variable on the garbage collected
heap to avoid dangling pointer errors.
Ask Compiler
When building the program, we can find out where the compiler stores all the variables,
whether in stack or heap.
A slice, on the other hand, is a data structure describing a contiguous section of the
array.
A slice is not an array.
Rather, it describes a piece of the array.
Slice is represented in memory with three elements…
- Length
- Capacity
- Pointer to the first element in the backing array
Go’s compiler will automatically create the array backing this slice.
Consider this example
When we create a slice with some initial values, the slice header will have the same
value and capacity.
0xc1030 is the address of the first element in the backing array
2 is the length and capacity.
Now, if we try to append a new element in this slice, there is not enough storage in the
backing array as shown in the diagram.
So, Go will allocate a new backing array twice the current capacity and copy the existing
elements to it.
The garbage collector will free the memory of the old array.
String, Bytes and Runes
Different Ways to Write Strings
Internals of String
String is basically a slice of byte.
The first field is the pointer to the first element in the underlying array of byte.
The Len field is the length of bytes.
This is the result for this code snippet.
The length returned is 9, but in fact the length of the string from a human’s perspective
is only 6.
This is because string is backed by an array of bytes internally.
And the emoji is represented in 4 bytes and the rest of the characters in 1 byte.