1. Introduction & Setup

Go (or Golang) is a programming language created by Google. It's known for its simplicity, efficiency, and powerful support for concurrency (Goroutines and Channels).

To start, install Go from the official site (go.dev). We'll use `fmt.Println` to print to the console.

package main import "fmt" func main() { fmt.Println("Hello, Go!") }
go run main.go

2. Variables & Data Types

Go is a statically typed language. You can use `var` for declaration, or `:=` for a short declaration inside functions.

func main() { // Standard declaration var age int = 30 // Short declaration (most common) name := "Budi" isReady := true fmt.Println(name, age, isReady) }

3. Arrays, Slices, & Maps

Go has three main collection data structures:

1. Array: Fixed-size. Rarely used directly.

2. Slice: Dynamic and much more common. It's a 'view' into an array.

3. Map: Key-Value pairs (like a Dictionary in Python).

// Slice (dynamic) mySlice := []string{"Apple", "Orange"} mySlice = append(mySlice, "Mango") // Map (Key-Value) myMap := make(map[string]int) myMap["age"] = 25

4. Control Flow (If & For)

Go is very minimal. It only has `if/else` and `switch` for conditionals, and ONLY `for` for looping (no 'while').

if age > 18 { fmt.Println("Adult") } else { fmt.Println("Child") } // Standard 'for' loop for i := 0; i < 5; i++ { fmt.Println(i) } // 'for' acting like a 'while' loop sum := 1 for sum < 100 { sum += sum }

5. Functions

Functions in Go can return more than one value. This is the common pattern for returning a value and an 'error' at the same time.

package main import ( "fmt" "errors" ) // This function returns (int, error) func divide(a, b int) (int, error) { if b == 0 { return 0, errors.New("cannot divide by zero") } return a / b, nil // 'nil' means no error } func main() { if res, err := divide(10, 2); err == nil { fmt.Println("Result:", res) } }

6. Structs

Go does not have 'Classes'. Instead, it uses 'Structs' to group data. Methods can be attached to structs.

type Person struct { Name string Age int } // This is a 'Method' attached to 'Person' func (p Person) Greet() { fmt.Println("Hello, my name is", p.Name) } func main() { p1 := Person{Name: "Alex", Age: 40} p1.Greet() }

7. Pointers

Pointers store the memory address of a variable. In Go, this is used to allow functions to modify the original value, or for efficiency.

`&` takes the address, `*` gets the value at that address (dereferencing).

func addFive(num *int) { *num = *num + 5 // Modify the original value } func main() { x := 10 addFive(&x) // Send the memory address of x fmt.Println(x) // Output: 15 }

8. Interfaces

Interfaces in Go are collections of method signatures. A type (like a struct) implements an interface *implicitly* (automatically) if it has all the required methods.

// The Interface type Shape interface { Area() float64 } // A Type type Rectangle struct { Width float64 Height float64 } // The Implementation (automatic) func (r Rectangle) Area() float64 { return r.Width * r.Height }

9. Goroutines

A Goroutine is a very lightweight 'thread' managed by the Go runtime. To run a function in a new goroutine, just use the `go` keyword.

import ( "fmt" "time" ) func sayHello() { fmt.Println("Hello from Goroutine!") } func main() { go sayHello() // Run in the background fmt.Println("Hello from Main!") time.Sleep(1 * time.Second) // Wait for the goroutine to finish }

10. Channels

Channels are the pipes used for communication *between* Goroutines. This is the safe way to send and receive data.

func main() { // Create a channel for strings messages := make(chan string) // Goroutine sends data to the channel go func() { messages <- "ping" }() // Main thread receives data from the channel msg := <-messages fmt.Println(msg) // Output: ping }

Practice Set

Reinforce basic Go concepts, from syntax, structs, to concurrency.

Question #1 - Go Introduction

What is the command to run a Go file and see the result immediately?

Question #2 - Variables

Which is the most common and concise way to declare AND initialize a variable inside a Go function?

Question #3 - Data Types

What is the main difference between an Array and a Slice in Go?

Question #4 - Looping

What is the *only* keyword for looping that exists in Go?

Question #5 - Functions

How does a Go function return TWO values (an integer and an error)?

Question #6 - Structs

How do you define a 'blueprint' for custom data (like a 'Class' in other languages) in Go?

Question #7 - Concurrency

What keyword is used to run a function concurrently on a lightweight thread?

Question #8 - Concurrency

How do Goroutines communicate with each other safely?

Final Exam

Comprehensively test your Go knowledge, including pointers and interfaces.

Question #1 - Go Introduction

What is the command to run a Go file and see the result immediately?

Question #2 - Variables

Which is the most common and concise way to declare AND initialize a variable inside a Go function?

Question #3 - Data Types

What is the main difference between an Array and a Slice in Go?

Question #4 - Looping

What is the *only* keyword for looping that exists in Go?

Question #5 - Functions

How does a Go function return TWO values (an integer and an error)?

Question #6 - Structs

How do you define a 'blueprint' for custom data (like a 'Class' in other languages) in Go?

Question #7 - Concurrency

What keyword is used to run a function concurrently on a lightweight thread?

Question #8 - Concurrency

How do Goroutines communicate with each other safely?

Question #9 - Pointers

What symbol is used to get the memory address of a variable?

Question #10 - Interfaces

How does a 'struct' in Go 'implement' an 'interface'?