GOLANG
The Golang programming language was built to fill in the gaps of C++ and Java that Google came across while working with its servers and distributed systems.
Top reasons you should learn Go Lang programming
- Go Programming Language has First-class functions
- Go language has a Built-in garbage collection
- Golang has Simple scoping rules
- Go Programming Language has Focused vocabulary
- Go has goroutines
It handles errors and bugs at the compile time which makes developers so comfortable using this language.
- It is compiled and statically typed programming language developed by Google.
- Statically typed means variable types are checked before by the compiler so type errors are picked up earlier.
- It's also strongly typed language which means variable type cannot be changed after it is declared.
- useful for writing lightweight microservices.
Goroutines: These are concurrent functions that run while the rest of the programs are complete. It allows multiple processes to run simultaneously and effectively.
For web apps, goroutines let you run concurrent jobs while avoiding roadblocks. The program will keep running even if the concurrent processes take longer than expected.
Only one thread in a program can be associated with thousands of Goroutines. Also if any Goroutine in the thread blocks, then another OS thread is created and the remaining Goroutines are moved to the new OS thread. All these are taken care of by the runtime
The syntax of the Go programming language is similar to the syntax of the C programming language.
Installation: https://go.dev/dl/
Here we use the main package to make the file executable.
The main package requires a main function that will be called when executing the file.
func main() is the entry point of the application. Go will automatically find and execute this after execution.
we can declare all other functions inside the main function to execute them.
we can only have one main function in our entry file (main.go)
Define Variables in Go
We use this only the first time while initializing or declaring the variable.
This is just a short-hand version. And we cannot use this shorthand outside of a function.
Printing & Formatting String
fmt -> format string and print them in console
→ new line
→ no new line
→ format string
Formatting String - Here we can put variables inside string.
%_ -> This is called format specifier
%v -> Default format specifier
%q -> string
%T -> get the type of that variable
%f -> float
%0.2f -> around 2 decimal points
%0.1f -> around 1 decimal point
fmt.Sprintf -> format and save the string somewhere, doesn't prints on the console.
Slices and Arrays
Array - In go, array has fixed length. We cannot change length once declared, nor type.
var myArray [5]int = [5]int{4,6,3,8,2}
Slice - A slice is a flexible and extensible data structure to implement and manage collection of data. All the elements within a slice are of same type. A slice is a segment of dynamic array that can grow and shrink. So we can append items to slice but not in array.
var mySlice = []int{4,6,8}
Function
We can declare the function anywhere but need to call that function in main function to execute it.
x, y := myFunction(2,4)
_, y := myFunction(2,4)
Naming Convention :
- Functions with names start with an uppercase letter will be exported to another packages.
- If the Function name starts with a lowercase letter,then it won’t be exported to another packages but still can be called in the same package
Pass by Value :
Go is known as pass by value language.
Non-Pointer Values :
- String
- Int
- Float
- Boolean
- Array
- Struct
- Maps
- Slices
- Functions
To define pointer :
var pName = &name
To get the value of pointer :
They can not be called unlike normal functions.
Comments
Post a Comment