DevOps - Step By Step Learning : Part 4 (Basic Programming Using Golang)
Saiful Islam Rasel
Senior Engineer, SDE @ bKash | Ex: AsthaIT | Sports Programmer | Problem Solver | FinTech | Microservice | Java | Spring-boot | C# | .NET | PostgreSQL | DynamoDB | JavaScript | TypeScript | React.js | Next.js | Angular
Story:
The roadmap defined was done, and it's time to start learning accordingly. The first step was learning a programming language. Though Rasel was working on C# (.NET), he decided to learn a new lightweight and faster language and picked Golang. Because Golang is lightweight, faster, and cloud-native. Many DevOps tools, like Docker and Kubernetes, are built using Golang.
As Rasel knows the programming fundamentals, it was easy to cross-check and learn it in the earliest time.
Programming Fundamentals:
In every programming language, basically the following fundamental things are the backbone:
How to these Concepts Works in Golang:
Data Types, Variables, and Constants
package main
import "fmt"
func main() {
var age int = 25 // Integer variable
name := "John" // String variable (short-hand)
const pi float64 = 3.14 // Constant value
fmt.Println("Age:", age)
fmt.Println("Name:", name)
fmt.Println("Pi:", pi)
}
Operators
package main
import "fmt"
func main() {
a, b := 10, 5
fmt.Println("Addition:", a+b) // Arithmetic
fmt.Println("Greater than:", a > b) // Relational
fmt.Println("Logical AND:", (a > 5) && (b < 10)) // Logical
}
Condition (if-else & switch)
package main
import "fmt"
func main() {
num := 10
if num > 0 {
fmt.Println("Positive number")
} else if num < 0 {
fmt.Println("Negative number")
} else {
fmt.Println("Zero")
}
// Switch case
day := 3
switch day {
case 1:
fmt.Println("Monday")
case 2:
fmt.Println("Tuesday")
default:
fmt.Println("Other day")
}
}
Loop (for loop)
package main
import "fmt"
func main() {
// Standard for loop
for i := 1; i <= 5; i++ {
fmt.Println("Iteration:", i)
}
// Loop through an array
numbers := []int{10, 20, 30}
for index, value := range numbers {
fmt.Println("Index:", index, "Value:", value)
}
}
Function
package main
import "fmt"
// Function with parameters and return value
func add(a int, b int) int {
return a + b
}
func main() {
sum := add(10, 5)
fmt.Println("Sum:", sum)
}
Memory Allocation and Pointers
package main
import "fmt"
func main() {
x := 10
p := &x // Pointer to x
fmt.Println("Value of x:", x)
fmt.Println("Memory address of x:", p)
fmt.Println("Value via pointer:", *p) // Dereferencing
}
Variable Lifetime and Scope
package main
import "fmt"
// Global variable
var globalVar = "I am global"
func main() {
localVar := "I am local" // Local variable
fmt.Println(globalVar)
fmt.Println(localVar)
}
Object-Oriented Concepts (Structs & Methods)
package main
import "fmt"
// Struct definition
type Car struct {
brand string
year int
}
// Interface definition
type Speaker interface {
Speak() string
}
// Method associated with Car struct
func (c Car) display() {
fmt.Println("Brand:", c.brand, "Year:", c.year)
}
func main() {
myCar := Car{"Toyota", 2022}
myCar.display() // Calling method
}
Data Structures
package main
import "fmt"
func main() {
// Array
var arr = [3]int{1, 2, 3}
fmt.Println("Array:", arr)
// Slice
slice := []int{10, 20, 30}
slice = append(slice, 40) // Append new element
fmt.Println("Slice:", slice)
// Map (key-value pairs)
person := map[string]int{"Alice": 25, "Bob": 30}
fmt.Println("Bob's age:", person["Bob"])
}
Algorithm (Sorting Example)
package main
import "fmt"
func bubbleSort(arr []int) {
n := len(arr)
for i := 0; i < n-1; i++ {
for j := 0; j < n-i-1; j++ {
if arr[j] > arr[j+1] {
arr[j], arr[j+1] = arr[j+1], arr[j]
}
}
}
}
func main() {
numbers := []int{64, 25, 12, 22, 11}
bubbleSort(numbers)
fmt.Println("Sorted array:", numbers)
}
Concurrency (Goroutines & Channels)
package main
import (
"fmt"
"time"
)
// Goroutine function
func sayHello() {
time.Sleep(2 * time.Second)
fmt.Println("Hello from Goroutine!")
}
func main() {
go sayHello() // Runs concurrently
fmt.Println("Main function execution")
time.Sleep(3 * time.Second) // Wait to see goroutine output
ch := make(chan int) // channel
go func() {
ch <- 42
}()
fmt.Println(<-ch) // receive from channel
}
Besides all of this, error handling, testing, generics are also advanced topics should be learned.
Summary:
Go (Golang) is a simple and efficient programming language with strong support for concurrency and performance. It has basic data types like int, string, bool, and float64, and allows you to store values in variables or constants (which don’t change). You can perform calculations and comparisons using operators such as +, -, ==, and &&. To make decisions, Go provides conditional statements like if, else, and switch, while repetitive tasks can be handled using loops, mainly the for loop.
Functions help break code into reusable blocks, and Go uses structs, methods and interfaces instead of traditional classes for object-oriented programming. Go also provides pointers to store memory addresses, and variables have lifetime and scope, meaning they exist for a certain time and in a certain part of the program. Data in Go can be organized using data structures like arrays, slices, maps, and structs, while algorithms define step-by-step solutions to problems.
A standout feature of Go is concurrency, allowing multiple tasks to run at the same time using goroutines and channels. This makes Go ideal for building fast, scalable applications. Overall, Go is designed to be simple, fast, and highly efficient, making it great for web development, cloud computing, and system programming.
Previous Parts: