Topics Discussed:
- Basic constructs and primitive data types
- Control structures
- Packages
- Arrays and Slices
- Maps
- Functions and Methods
- Structs
- Interfaces
- Goroutines and Channels
- Error handling
Introduction
Almost all of the DevOps tools developed in Go.
For a complex task python is slow to process.
Total 25 keywords in Golang and it’s the base of all those complex codes written in the industry.
Go will force a developer to write code in a complex way.
No compilation complexities with exceptional backward complexity.
Golang
Introduced by Google
Designed by Robert Griesmer, Rob Pike and Ken Thompson
Open Source version released in 2009
Go language is easy to use in advanced programming compared to Golang.
Slowness in python as it works using external packages and package dependencies
Key features:
• Statically typed
• Simplicity
• Its compiled language
• Cross compilation . We don’t need a specific OS platform to develop a binary
• Garbage collection. Memory management
• Built-in concurrency
• Compile to standalone libraries
Variables
Variable will define using a keyword var.
Keywords
25 keywords
Identifiers
Starts with letter or _
_ or blank identifier
Basic data types:
Two types
Primitive data types and derived data types
Basic
Numeric : intiger, floating point, complex
Boolian : default false
Text : string
Derived
Composite : array,structs etc
Constants
Constants will not change during a program execution
Typed constants and untyped constants
Packages
All go files need to be defined in a package
We can import additional package specifing package path URL
Its a way to structure the code
Control structures
Conditional and branching structures and iterative looping structure.
Similar to other programming languages like go have if, if else, for, etc..
Switch case:
In go switch case does not have any break statement
While and do while is not there in go.
While loop can handle using for itself in go.
Arrays
Specify length while initialize and array
Slice
default value will be Nill
Slice is a reference to a portion of an array
Slice in memory is a structure with 3 fields
A pointer to underlying array
Length of slice
Capacity of the slice
Maps
Unordered collection of pairs of items
Key value pairs
Keyword: map
Pointers
Its a variable that stores memory address of other variable
Default value is nil
Does not support pointer arithmetic
Nil pointer dereference is not allowed
Structs
Heterogeneous data type
Collection of fields
Struct field accessed using a dot
Struct fields can be accessed through a struct pointer
Embedded struct – can assign other struct inside a struct
Functions and Methods
Function can take zero or more arguments
Method is a function act on a variable of a certain type called receiver
Value receivers and pointer receivers
Interfaces
Its a functional language
A way to specify behaviour of an object
Groutines
Building blocks of structuring concurrent programs are goroutines and channels
Goroutine is a lightweight thread managed by go runtime
Possible to run multiple threads in go routine
Channels
Used to communicate between goroutines
Error handling
Does not have an exception handling
Can’t throw exceptions
Is there any relation between Go and Go template
Ans : Go templating is framework
References:
• https://tour.golang.org/welcome/1
• https://golang.org/doc/effective_go
• https://golang.org/doc/
• https://go.dev/blog/
• https://golang.org/doc/faq
• http://www.golangbootcamp.com/book/
• https://gobyexample.com/
• https://www.udemy.com/course/learn-how-to-code/
• https://github.com/golang/go/wiki/GoUsers