🐹 Go

Introduction to Go: history, features and applications

What is it and why?

Imagine you are building a house out of Lego bricks. There are bricks that simply stack on top of each other — that's like a simple programming language. And there are bricks that also lock together tightly so the house doesn't fall apart, even if a ball hits it. Go (or Golang) is like "super-strong Lego" for programmers. It was invented at Google in 2009 because programmers were tired of waiting for their programs to start up slowly or crash under heavy load.

Why should you learn Go? Imagine you are writing a program for an online game where a million people play simultaneously. In Python or JavaScript, such a program might start to lag or crash. Go is designed to work quickly and reliably, like a Swiss watch. It is used in large projects: Docker (a program for running applications), Kubernetes (a system for managing programs), as well as in the servers of Telegram and YouTube. If you want to become a cool developer who can make fast and powerful programs — Go is your choice.

Syntax and code examples

Go looks strict but logical. Let's break down a few examples. Don't be scared — I'll explain every line!

Example 1: Hello, World!

package main // 1. We say that this is the main program

import "fmt" // 2. We take the library for text output

func main() { // 3. The main function, everything starts here
    fmt.Println("Hello, World!") // 4. Print the text
}

Comments:

  • package main — every Go program starts by specifying a "package". main is like the "main entrance" to the program.
  • import "fmt" — we include a ready-made set of tools (library) for working with text. fmt is short for "format".
  • func main() — the function that runs first. We write commands inside it.
  • fmt.Println(...) — the command "print the string and go to a new line". Everything in quotes will appear on the screen.

Example 2: Variables and Math

package main

import "fmt"

func main() {
    // Declare variables
    var age int = 14          // age is an integer (int)
    var name string = "Anna"   // name is a string (string)
    var height float64 = 1.65 // height is a floating-point number (float64)

    // Calculate age in 5 years
    futureAge := age + 5 // := is a short way to declare new variables

    // Print the result
    fmt.Println("Hello,", name)
    fmt.Println("You are now", age, "years old")
    fmt.Println("In 5 years you will be", futureAge)
    fmt.Println("Your height:", height, "m")
}

Comments:

  • var age int = 14 — create a variable age of type int (integer) and put 14 in it.
  • futureAge := age + 5 — a short way to create a variable. Go will figure out that futureAge is also an integer.
  • fmt.Println can print several things at once, separated by commas.

Example 3: Conditions (if-else)

package main

import "fmt"

func main() {
    var age int = 16

    if age >= 18 { // If age is greater than or equal to 18
        fmt.Println("You are an adult!")
    } else if age >= 12 { // Else if age >= 12
        fmt.Println("You are a teenager!")
    } else { // Otherwise (all other cases)
        fmt.Println("You are a child!")
    }
}

Comments:

  • if — checks a condition. If it is true, the code inside the curly braces {} is executed.
  • else if — another check, if the first one didn't match.
  • else — executed if none of the conditions matched.
  • Note: In Go, there are no parentheses around the condition (like in Python), but curly braces are mandatory!

Example 4: Loop (for)

package main

import "fmt"

func main() {
    // Count from 1 to 5
    for i := 1; i <= 5; i++ {
        fmt.Println("Count:", i)
    }

    // Infinite loop (stops when i becomes 10)
    i := 0
    for {
        i++
        if i == 10 {
            break // exit the loop
        }
    }
    fmt.Println("Loop finished, i =", i)
}

Comments:

  • for i := 1; i <= 5; i++ — three parts: initial value (i=1), condition (while i <= 5), step (increase i by 1).
  • i++ — shorthand for i = i + 1.
  • break — the command "stop, exit the loop".
  • Go does not have a while loop like Python. Instead, for with a single condition is used.

Example 5: Functions

package main

import "fmt"

// Function that adds two numbers
func add(a int, b int) int {
    return a + b // return the result
}

func main() {
    result := add(5, 3) // call the function
    fmt.Println("5 + 3 =", result)

    // Can be called multiple times
    fmt.Println("10 + 20 =", add(10, 20))
}

Comments:

  • func add(a int, b int) int — declare a function named add. It takes two integers (a and b) and returns an integer (the last int).
  • return a + b — return the sum.
  • Functions help avoid writing the same code many times.

Comparison with Python

Python is like a "spoken language": it's easy and quick to write in, but it can be slow. Go is like an "official document": strict, but fast and reliable. Let's compare: