This website requires JavaScript.
๐Ÿ“œ Basics
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
var <variable-name> <data-type>
<variable-name> := <value> # Short declaration, type inferred

# Example
var i int  // declaring int variable at functional level
j := 99 // short assignment without var declaration
๐Ÿ”’ Constants
const Pi = 3.14
const (
    A = 1
    B = "Hello"
)
๐Ÿ”ข Data types

Table 1: Integer Data Types

Data typeDescriptionSizeRangeAliasuint88-bit unsigned integer1 byte0 to 255byteint88-bit signed integer1 byte-128 to 127N/Aint1616-bit signed integer2 bytes-32768 to 32767N/Auint1616-bit unsigned integer2 bytes0 to 65,535N/Aint3232-bit signed integer4 bytes-2,147,483,648 to 2,147,483,647int (on 32-bit systems), runeuint3232-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)

Table 2: Floating Point Data Types

Data typeDescriptionSizeAliasfloat3232-bit signed floating point number4 bytesN/Afloat6464-bit signed floating point number8 bytesfloatcomplex64float32 real and imaginary8 bytesN/Acomplex128float64 real and imaginary16 bytesN/A

Table 3: Boolean Data Type

Data typeSizeAliasbool1 bytetrue or false

Table 4: String Data Type

Data typeDescriptionstringsequence of charactersrunealias 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: =, +=, -=, *=, %=
๐Ÿ”ค String Functions
  • len(str): returns the length of the string.
  • strings.Compare(a, b): compares two strings.
  • strings.Contains(str, substr): checks if substr is in str.
  • strings.ToUpper(str): converts str to uppercase.
  • strings.ToLower(str): converts str to lowercase.
  • strings.HasPrefix(str,"prefix"): checks if str starts with "prefix".
  • strings.HasSuffix(str,"suffix"): checks if str ends with "suffix".
  • strings.Index(str, substr): returns the index of substr in str.
  • strings.Join([]string, sep): joins a slice of strings with sep.
  • strings.Count(str, sep): counts occurrences of sep in str.
๐Ÿ—‚ Arrays
var fruits [3]string
fruits[0] = "Mango"
fruits[1] = "Apple"
fruits[2] = "Orange"
arr := [...]int{1,2,3,4,5}
๐Ÿ“ Slice
arr := [5]int{1, 2, 3, 4, 5}
slice := arr[1:4]
slice = append(slice, 6)
๐Ÿ—บ Maps
m := make(map[string]int)
m["foo"] = 1
delete(m, "foo")
๐Ÿ”€ Conditional Statements
if cond { ... }
if cond { ... } else { ... }
if cond1 { ... } else if cond2 { ... } else { ... }

switch val {
case 1:
    ...
case 2, 3:
    ...
default:
    ...
}
๐Ÿ” Loops
for i := 0; i < 5; i++ { ... }          # classic for
for i < 5 { ... }                       # while-like
for { ... }                             # infinite loop
๐Ÿ›  Functions
func add(a int, b int) int {
    return a + b
}
sum := add(1, 2) # can return multiple values

func swap(a, b string) (string, string) {
    return b, a
}
๐Ÿ“ Pointers
num := 10
var ptr *int
ptr = &num
fmt.Println(*ptr) // dereference
๐Ÿ‘ค Structures
type Person struct {
    Name string
    Age  int
}

p := Person{Name: "John", Age: 30}
โšก Concurrency

1. Go-routines

go someFunction()

2. Channels

ch := make(chan int)
go func() { ch <- 5 }()
x := <-ch

3. Buffered Channels

ch := make(chan int, 2)

4. Select

select {
case x := <-ch1:
    ...
case y := <-ch2:
    ...
default:
    ...
}
โณ Defer, Panic & Recover
defer fmt.Println("Executed last")

if err != nil {
    panic("Something went wrong")
}

defer func() {
    if r := recover(); r != nil {
        fmt.Println("Recovered from", r)
    }
}()
๐Ÿงฉ 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--
    }
}
๐Ÿงฑ Interface
type MyTypes interface {
    int | string | bool
}
func ReverseSlice[T MyTypes](slice []T) {
    // ...
}
๐Ÿ“ฆ Packages & Modules
go mod init mymodule
go get github.com/user/repo
go run main.go
go build
๐Ÿงช Testing
import "testing"

func TestAdd(t *testing.T) {
    got := add(2, 3)
    want := 5
    if got != want {
        t.Errorf("got %d, want %d", got, want)
    }
}
Git
HTML