This website requires JavaScript.

Golang

Go language is an open-source, statically typed programming language by Google. Go is highly recommended for creation of highly scalable and available web applications.

Basics

Sample Go program

package main
import "fmt"

func main() {
    fmt.Printf("Go Hello, World!")
}

package -- package declaration which is mandatory for all Go programs. import "fmt" -- used to import built-in fmt package. func main() -- function is the starting point of execution of any Go program. fmt.printf -- inbuilt library function which is used to print the given message. fmt.Scanf -- inbuilt library function which is used to read the data. // -- to comment a single line /**/ -- to comment multiple lines

Variables

Declaring Variables

var variable-name data-type Assigning value to variables variable-name := value // It's not mandatory to declare a variable when you use this. Example

var i int  // declaring int variable at functional level
j := 99 // short assignment without var declaration

Data types

1. Numeric Data types

Integer Data types

Data typeDescriptionSizeRangeAlias
uint88-bit unsigned integer1 byte0 to 255byte
int88-bit signed integer1 byte-128 to 127N/A
int1616-bit signed integer2 bytes-32768 to 32767N/A
unit1616-bit unsigned integer2 bytes0 to 65,535N/A
int3232-bit signed integer4 bytes-2,147,483,648 to 2,147,483,647int (on 32-bit systems), rune
uint3232-bit unsigned integer4 bytes0 to 4,294,967,295uint (on 32-bit systems)
int6464-bit signed integer8 bytes-9e18 to 9e18int (on 64-bit systems)
uint6464-bit unsigned integer8 bytes0 to 18e18uint (on 64-bit systems)

Float Data types

Data typeDescriptionSizeAlias
float3232-bit signed floating point number4 bytesN/A
float6464-bit signed floating point number8 bytesfloat
complex64float32 real and imaginary8 bytesN/A
complex128float64 real and imaginary16 bytesN/A
2. Boolean

bool — 1 byte — true or false

3. String

string — sequence of characters rune — alias for uint32, represents single char

Example

var (
    integer uint32 = 4294967295
    flt float64 = 3.14
    complexNum complex128 = cmplx.Sqrt(8 - 6i)
    str string = "Hello World"
    rne rune = 'A'
    boolean bool = true
)

Operators

Arithmetic: +, -, *, /, % Comparison: <, >, <=, >=, !=, == Bitwise: &, ^, |, &^, <<, >> Logical: &&, ||, ! Assignment: =, +=, -=, *=, etc.

String Functions

len(str), strings.Compare(a, b), strings.Contains(str, substr), strings.ToUpper(str), strings.ToLower(str), strings.HasPrefix(str,"prefix"), strings.HasSuffix(str,"suffix"), strings.Index(str, substr), strings.Join(string, sep), strings.Count(str, sep)

Arrays

var fruits [3]string
fruits[0] = "Mango"
fruits[1] = "Apple"
fruits[2] = "Orange"
arr := [...]int{1,2,3,4,5}

Conditional Statements

if, if-else, else-if ladder, switch

if cond { ... }
if cond { ... } else { ... }
switch val { case 1: ... default: ... }

Loops

for, for as while, infinite for + break

Functions

func add(a int, b int) int { return a + b }
add(1, 2)

Pointers

num := 10
var ptr *int
ptr = &num

Structures

type Person struct {
    name string
    age int
}

Slice

arr := [5]int{1, 2, 3, 4, 5}
slice := arr[1:4]

Maps

m := make(map[string]int)
m["foo"] = 1

Concurrency

  1. Go-routines
go someFunction()
  1. Channels
ch := make(chan int)
ch <- 5
x := <-ch

Generics (Go 1.18+)

func ReverseSlice[T int | string | bool](slice []T) {
    left, right := 0, len(slice)-1
    for left < right {
        slice[left], slice[right] = slice[right], slice[left]
        left++
        right--
    }
}

Using interface

type MyTypes interface {
    int | string | bool
}
func ReverseSlice[T MyTypes](slice []T) {
    // ...
}
Git
HTML