GoLang Cheatsheet

GoLang Cheatsheet

April 1, 2023

Golang Cheat Sheet: Go, also known as Golang, is a statically-typed and compiled programming language developed by Google. This cheat sheet provides a quick reference to the most commonly used Go syntax and concepts.

Basics

Variables

var myVar // Variable Declaration
int myVar = 5 // Variable Assignment
anotherVar := 10 // Declaration & Assignment (Shorthand)

var myFloat float64 = 3.14 // Float
var myString string = "Hello, World!" // String 
var myBool bool = true // Boolean
const Pi = 3.14

var a, b, c int  // Multiple Variable Declarations
a, b, c = 1, 2, 3 // Multiple Assignments
x, y := 4, "hi"  // Multiple Shorthand Assignments

Control Structures

If-Else

if x < y {
fmt.Println("x is less than y")
} else if x > y {
fmt.Println("x is greater than y")
} else {
fmt.Println("x is equal to y")
}

For Loop

for i := 0; i < 10; i++ { fmt.Println(i)
}

Switch

switch day {
    case "Mon":
        fmt.Println("Monday")
    case "Tue", "Wed", "Thu", "Fri":
        fmt.Println("Weekday")
    case "Sat", "Sun":
        fmt.Println("Weekend")
    default:
        fmt.Println("Invalid day")
}

Functions

func myFunction() {
fmt.Println("Hello from myFunction!")
}

func add(x int, y int) int {
    return x + y
}

func swap(x, y string) (string, string) {
    return y, x
}

Data Structures

Pointers

Error Handling