Go in bits
GoDevelopmentBackend

Go in bits

Archive of all the links from my socials for go tuts.

Why?

  • I'm bored & wanted to do some low level coding for a long time.
  • I worked with someone who build a backend in go it was fast AF! like less than 20 ms for an api which was more than 200 ms on js/ts.

that's why i'm learning go!

I'll attach links and resources i'm from as we progress. Bookmark this blog for later.

1. Hello World in Go

package main

import "fmt"

func main() {
	fmt.Println("Hello sir")
}

To run this just run this following command :

go run ./filname.go

If you want to build this then just

go build ./filaname.go

you will get a asm file just do

./filaname

2. Simple Values & Variables in GO

package main

import "fmt"

func main() {
	var age = 20

	if age >= 18 {
		fmt.Println("i'm an adult")
	} else {
		fmt.Println("i'm a kid")
	}

	//we do not have ternary op like js have
}

3. Constants, short hand syntax & const group

package main

import "fmt"

const age = 30

const (
	PORT = 3000
	URL  = "http://localhost"
)

func main() {

	name := "ram"
	fmt.Println(name)
	fmt.Println(age)
	fmt.Println(PORT)
	fmt.Println(URL)
}

4. Loops in go

  • For loop
  • Infinite loop
  • While loop
  • Range loop (don't use it on integers its a bad practice)
package main

import "fmt"

// for -> is the only construct go have for looping there is no other kind of loops

func main() {

// comment other loops and use only loop at a time 
// otherwise infinite loop will block scope of range loop

	// While loop
	i := 0

	for i <= 3 {
		fmt.Println(i)
		i = i + 1
	}

	// common for loop syntax like c++ and js
	for j := 0; j <= 3; j++ {
		fmt.Println(j)
	}

	// Infinite loop
	for {
		fmt.Println("ramxcodes")
	}

	// range loop but you should not use on integers 
       // like i'm doing right now it only be used on arrays and slices

	for x := range 3 {
		fmt.Println(x)
	}
}

5. If else in go

  • If else
  • Nested If else
  • && and || operators
package main

import "fmt"

func main() {
	age := 22

	if age >= 18 {
		fmt.Println("adult")
	} else if age >= 12 {
		fmt.Println("teenagaer")
	} else {
		fmt.Println("kid")
	}
	

	var role = "admin"
	var hasPermissions = false

	if role == "admin" || hasPermissions {
		fmt.Println("Access Granted!")
	} else {
		fmt.Println("No Access")
	}

	// we do not have ternary op ? like js have
	// so we need to use if else
}

6. Switch cases

  • Simple Switch case
  • Multiple Condition Switch
  • Type Switch
package main

import (
	"fmt"
	"time"
)

func main() {
	// simple switch

	i := 2

	switch i {
	case 1:
		fmt.Println("one")
	// break
	case 2:
		fmt.Println("two")
	case 3:
		fmt.Println("three")
	default:
		fmt.Println(" other val ")
	}

	// multiple condition switch

	switch time.Now().Weekday() {
	case time.Saturday, time.Sunday:
		fmt.Println("Its weekend")
	default:
		fmt.Println("its a working day")
	}

	// type switch case

	WhoAmI := func(i interface{}) {
		switch t := i.(type) {
		case int:
			fmt.Println("its an integer")
		case bool:
			fmt.Println("its a bool")
		case string:
			fmt.Println("its a string")
		default:
			fmt.Println("this is ", t, "you can see")
		}
	}

	WhoAmI(234.324)
}

7 Array

  • Simple array
  • Push element in a array
  • 2D Array
package main

import "fmt"

func main() {
	// Arrays are numbered sequence of a specific length

	var nums [6]int  // <- array declaration, default values will be 0.

	fmt.Println(len(nums)) // <- len is a length funcation comes inbuilt in go

	fmt.Println("--------")
    
    
	// Push

	nums[0] = 1 
	// pushed 1 at 0th index
	nums[1] = 4 
	// pushed 4 at 1st index

	fmt.Println(nums)
	fmt.Println(nums[0])
	fmt.Println(nums[1])
	fmt.Println("--------")

	// Bool array

	var vals [4]bool 
	// Default bool value is false

	fmt.Println(vals)
	fmt.Println("--------")

	// String array

	var names [4]string 
	// Default is empty string
	names[0] = "R"
	names[1] = "a"
	names[2] = "M"
	names[3] = "x"
	fmt.Println(names)

	// Decalre array and add elements like js
	fmt.Println("--------")

	numbers := [3]int{1, 2, 3}
	fmt.Println(numbers)

	// 2-D arrays
	fmt.Println("--------")

	number := [2][2]int{{1, 2}, {3, 4}}
	fmt.Println(number)

}

8 Slices

  • How Slices work
  • Push element to a slice
  • 2D Slice
  • Copy a slice
  • Slice Function
package main

import (
	"fmt"
)

func main() {

	var nums = make([]int, 0, 5)

	nums = append(nums, 1)
	nums = append(nums, 2)
	nums = append(nums, 3)
	nums = append(nums, 4)
	nums = append(nums, 5)
	nums = append(nums, 6)

	var nums2 = make([]int, len(nums))
	copy(nums2, nums)
	fmt.Println(nums2)
	fmt.Println(nums)

	var numx = []int{1, 2, 3}
	var numy = []int{1, 2, 3}

	// slice package
	fmt.Println(slices.Equal(numx, numy))

	var nums2D = [][]int{{1, 2}, {3, 4}}
	fmt.Println(nums2D)

	// slice operator

	var nums3 = []int{0, 1, 2, 3}
	fmt.Println(nums3[:])
}

9 Maps

  • How map work
  • Insert, Delete, Clear
  • Maps package
  • _, ok syntax to safely access the value
package main

import (
	"fmt"
	"maps"
)

func main() {
	m := make(map[string]string)
	m["name"] = "ram"
	m["x"] = "ramxcodes"
	m["github"] = "@ramxcodes"

	fmt.Println(m["phone"])

	map2 := make(map[string]int)
	map2["age"] = 30
	fmt.Println(len(map2))
	fmt.Println(m)
	delete(m, "x")
	fmt.Println(m)
	clear(m)
	fmt.Println(m)
	

	map3 := map[string]int{"price": 40, "phone": 123}
	fmt.Println(map3)

	v, ok := map3["x"]
	fmt.Println(v)
	if ok {
		fmt.Println("price exists")
	} else {
		fmt.Println("not sure")
	}

	map4 := map[string]int{"price": 40, "x": 32}
	map5 := map[string]int{"price": 40, "x": 32}

	fmt.Println(maps.Equal(map4, map5))
}

10 Range

  • How range works
  • How to iterate over maps
  • How to iterate over string
  • How to iterate over maps and perform sum

package main

import "fmt"

// range -> iterating over data structure

func main() {

	// slice
	nums := []int{5, 6, 7, 8}

	for i := 0; i < len(nums); i++ {
		fmt.Println(nums[i])
	}
	
	// now with range

	for _, num := range nums {
		fmt.Println(num)
	}
	

	sum := 0
	for _, num := range nums {
		sum = sum + num
	}
	fmt.Println(sum)
	
	for i, num := range nums {
		fmt.Println(num, i)
	}

	// Map

	m := map[string]string{"fname": "ram", "lname": "codes"}

	for k, v := range m {
		fmt.Println(k, v)
	}

	//  string

	// i -> starting byte of rune, c -> unicode of charachter (rube ds)
	for i, c := range "ramxcodes" {
		fmt.Println(i, c)
	}
}

11 Functions

  • How function works
  • Function with parameters
  • Function without parameters
  • Passing Functions as Arguments
  • Returning Functions
package main

import "fmt"

func add(n int) int {
	return n
}

func getLang() (string, string, string, bool) {
	return "go", "Js", "c++", true
}

func applyOperation(n int, operation func(int) int) int {
	return operation(n)
}

func multiplier(factorial int) func(int) int {
	return func(n int) int {
		return n * factorial
	}
}

func main() {
	result := add(3 + 5)
	fmt.Println(result)

	lang1, lang2, lang3, isCool := getLang()

	fmt.Println(lang1)
	fmt.Println(lang2)
	fmt.Println(lang3)
	fmt.Println(isCool)

	double := func(x int) int {
		return x * 2
	}

	square := func(x int) int {
		return x * x
	}

	fmt.Println(applyOperation(5, double)) // prints 10
	fmt.Println(applyOperation(5, square)) // prints 25

	timesTwo := multiplier(2)
	timesThree := multiplier(3)

	fmt.Println(timesTwo(5))
	fmt.Println(timesThree(5))
}

12 variadic function

  • Variadic function
  • spread operator
package main

import "fmt"

func sum(nums ...int) int {
	total := 0

	for _, num := range nums {
		total = total + num
	}

	return total
}

func main() {

	nums := []int{1, 2, 3, 4, 5}
	result := sum(nums...)

	fmt.Println(result)
}

Related Posts

Next JS Data Fetching mistakes & Security vulnerabilities

Next JS Data Fetching mistakes & Security vulnerabilities

A guide to Next.js data fetching mistakes & security vulnerabilities

DevelopmentFrontendNext.Js
Read More
JavaScript for Frontend Development: A Beginner's Guide

JavaScript for Frontend Development: A Beginner's Guide

A guide to JavaScript for frontend development for beginners part 1

DevelopmentFrontendJavaScript
Read More
Routing in Next.js (App Router) - A Complete Guide (2025)

Routing in Next.js (App Router) - A Complete Guide (2025)

A guide to routing in Next.js (App Router) covering Catch-All Segments, Dynamic Routes, Nested Routes, and more.

DevelopmentNext.Js
Read More

Design & Developed by Ramxcodes
© . All rights reserved.