← Back to course

Free trial: Golang Developer

Complete a couple of real lessons from this course — no signup required.

01

Lesson 1: Packages, comments, variables & operators

Packages. Comments.

Any Go code lives inside a package. A package is a folder that contains .go files.

Let's look at the first simple example of a Go program:

main.go
package main

import "fmt"

func main() {
    fmt.Println("Salem Bitlab!")
}
Output:Salem Bitlab!
GOLANG
Output

To run this program we used the main and fmt packages.

package main is the main package. It must contain a func main() function, which is the program's entry point. When you run a Go application, this is exactly the function that gets called first.

package fmt is a standard package used for printing data to the console. Besides printing, it also supports formatted input and output.

As a program grows, its code becomes harder to read and understand. Comments are used to make the program's logic easier to follow.

Comments explain code and split it up logically. They don't take part in compilation and have no effect on how the program runs.

Types of comments

02

Lesson 2: Conditionals & Logical Operators

What are conditions in Go?

In real programs, code doesn't always run top to bottom in the same way. Sometimes the program has to make a decision: run one block of code, or another.

For example:

  • if the user's age is 18 or older — allow access;
  • if the password is correct — log in;
  • if the balance is enough — process the payment;
  • if the score is high — print a success message.

Go uses conditional statements for situations like these. They let the program choose which code to run.

A condition is an expression whose result is always true or false.

How does a condition work?

A conditional statement checks an expression. If the result is true, one block of code runs. If the result is false, that block is skipped, or a different block runs instead.

The if statement

The simplest conditional statement is if. It only runs its code when the condition is true.

main.go
package main

import "fmt"

func main() {
    age := 20

    if age >= 18 {
        fmt.Println("Access granted")
    }
}
Output:Доступ разрешён
GOLANG
Output

Here the program checks the condition age >= 18. The variable age equals 20, so the condition is true, which means the code inside the if block runs.

Important: in Go, the condition inside if isn't wrapped in parentheses. But the curly braces { } are required.

The if ... else statement

When you need to run one piece of code if a condition is true, and a different piece if it's false, you use if ... else.

main.go
package main

import "fmt"

func main() {
    age := 15

    if age >= 18 {
        fmt.Println("Access granted")
    } else {
        fmt.Println("Access denied")
    }
}
Output:Доступ запрещён
GOLANG
Output

age equals 15. The condition age >= 18 returns false. So the if block is skipped, and the else block runs instead.

03

Lesson 3: Loops & the for Statement

What is a loop?

A loop is a sequence of instructions that lets you perform an action many times.

In programming, you very often need to repeat the same action several times. For example:

  • printing the numbers from 1 to 10;
  • calculating the sum of a set of numbers;
  • reading several values from the console;
  • processing a list of students;
  • printing a multiplication table.

Without loops you'd have to write the same code over and over by hand. Loops make code shorter, clearer, and easier to work with.

Go only has one loop — for — but it can take several different forms.

How does a loop work?

  • first, a starting value is set;
  • then the condition is checked;
  • if the condition is true, the loop's body runs;
  • after that, the value changes;
  • then the condition is checked again.

The classic for

The classic for loop is used when you roughly know in advance how many times the loop needs to run.

For example, to print the numbers from 0 to 4:

main.go
package main

import "fmt"

func main() {
    for i := 0; i < 5; i++ {
        fmt.Println(i)
    }
}
Output:0 1 2 3 4
GOLANG
Output

The loop is made up of three parts:

  • i := 0 — initialization (the starting value)
  • i < 5 — condition (the loop keeps running while this is true)
  • i++ — step (updates the variable after each iteration)

So the loop ran 5 times — until i became equal to 5.

Example: printing 1 to 10

A very common task is printing numbers within a given range. A regular for loop handles that.

main.go
package main

import "fmt"

func main() {
    for i := 1; i <= 10; i++ {
        fmt.Println(i)
    }
}
Output:1 2 3 4 5 6 7 8 9 10
GOLANG
Output

In this example, i starts at 1. The loop runs as long as i <= 10. After each iteration, i increases by 1.

Example: sum of numbers

Loops are often used to accumulate a result. For example, we can add up the numbers from 1 to 5.

main.go
package main

import "fmt"

func main() {
    sum := 0

    for i := 1; i <= 5; i++ {
        sum = sum + i
    }

    fmt.Println(sum)
}
Output:15
GOLANG
Output

Here, the sum variable holds the running total. On each iteration we add the current value of i to it.

How sum changes:

  • i = 1, sum = 0 + 1 = 1
  • i = 2, sum = 1 + 2 = 3
  • i = 3, sum = 3 + 3 = 6
  • i = 4, sum = 6 + 4 = 10
  • i = 5, sum = 10 + 5 = 15
04

Lesson 4: Slices

What is a slice?

A slice is a sequential collection of elements of the same type. Unlike arrays, slices have a dynamic length: you can add new elements to them and remove existing ones.

Slices are very similar to arrays, but much more convenient in real development. While an array has a fixed size, a slice can grow as needed.

That's why in Go, people usually work with slices rather than plain arrays.

Slices are like a Kazakh toi (a big celebration): you can plan an event for 200 people, but how many guests actually show up in the end is anyone's guess. Maybe 150? 250? Or a full 300? Slices have the same quality as toi organizers — flexibility.

Meiram "Punctuality" Toibastarov — the guy who "never" shows up late, and knows a thing or two about slices

The main difference between a slice and an array — a slice's length can change while the program is running.

Array vs slice

Visually, an array and a slice look very similar, but there's an important difference between them.

Array

Fixed size. If an array is created for 5 elements, it will always contain exactly 5 elements.

Slice

Dynamic size. You can add elements to a slice and get a new, bigger slice.

In Go, a slice is written almost like an array, just without specifying a size inside the square brackets.

Creating a slice

Slices are created almost like arrays, just without specifying a length.

Main.go
package main

import "fmt"

func main() {
    boxers := []string{"Muhammad Ali", "Mike Tyson", "George Foreman", "Joe Frazier"}
    for _, value := range boxers {
        fmt.Println(value)
    }
}
Output:Muhammad Ali Mike Tyson George Foreman Joe Frazier
GOLANG
Output

Notice: inside [] we didn't specify a length — that's exactly why this is a slice.

If you write [4]string, that's an array. If you write []string, that's a slice.

Slices of different types

A slice can store elements of different data types, but inside one slice all elements must be of the same type.

A slice of numbers:

numbers := []int{10, 20, 30, 40}

A slice of strings:

cities := []string{"Almaty", "Astana", "Paris"}

A slice of booleans:

answers := []bool{true, false, true}