Go

Learn Go programming with simple, clear examples

CONTENTS

  1. Variables
  2. Numbers
  3. Strings
  4. Slices
  5. Functions
  6. Loops
  7. Conditions
  8. Structs
  9. Methods
  10. Error Handling
  11. Interfaces
  12. Final Project

Learn Go through examples. Each section shows a concept with runnable code.

Click the “Run” button to execute examples and see output.


Variables

Go is statically typed. Declare variables with var and specify the type.

var name string = "Alice"
var age int = 25
Go
Go

Numbers

Numbers are numbers. Add. Subtract. Multiply. Divide.

var x int = 10
var y int = 3
var sum int = x + y

Exercise

Calculate 15 * 4 and store it in a variable called result with type int.

Go

Strings

Strings are text. Words. Letters. Characters.

var greeting string = "Hello"
var name string = "World"
var message string = greeting + " " + name

Exercise

Create a string called sentence that says “Go is great”.

Go

Slices

Slices hold multiple items. Ordered. Indexed. Dynamic.

var fruits []string = []string{"apple", "banana", "orange"}
var firstFruit string = fruits[0]

Exercise

Create a slice called colors with type []string containing “red”, “green”, and “blue”.

Go

Functions

Functions do things. Take input. Return output. Typed.

func greet(name string) string {
    return "Hello, " + name
}

var message string = greet("Alice")

Exercise

Create a function called add that takes two integers and returns their sum.

Go

Loops

Loops repeat. For each item. Do something.

var numbers []int = []int{1, 2, 3, 4, 5}
for _, num := range numbers {
    fmt.Println(num * 2)
}

Exercise

Print each number from 1 to 5.

Go

Conditions

Conditions check. If true, do this. If false, do that.

var age int = 18
if age >= 18 {
    fmt.Println("Adult")
} else {
    fmt.Println("Minor")
}

Exercise

Check if a number is positive. Print “Positive” or “Negative”.

Go

Structs

Structs group data. Fields. Properties. Organized.

type Person struct {
    Name string
    Age  int
    City string
}

var person Person = Person{
    Name: "Alice",
    Age:  25,
    City: "New York",
}
fmt.Println(person.Name)

Exercise

Create a struct called Book with Title and Author fields.

Go

Methods

Methods belong to types. Functions. Attached. Organized.

type Person struct {
    Name string
}

func (p Person) Greet() string {
    return "Hello, I'm " + p.Name
}

var alice Person = Person{Name: "Alice"}
fmt.Println(alice.Greet())

Exercise

Create a struct called Car with a Brand field and a Start method.

Go

Error Handling

Errors happen. Check them. Handle them.

result, err := divide(10, 0)
if err != nil {
    fmt.Println("Error:", err)
} else {
    fmt.Println("Result:", result)
}

func divide(a, b int) (int, error) {
    if b == 0 {
        return 0, fmt.Errorf("cannot divide by zero")
    }
    return a / b, nil
}

Exercise

Create a function that returns an error if a number is negative.

Go

Interfaces

Interfaces define behavior. Methods. Contracts. Promises.

type Writer interface {
    Write([]byte) (int, error)
}

type ConsoleWriter struct{}

func (cw ConsoleWriter) Write(data []byte) (int, error) {
    return fmt.Print(string(data))
}

Exercise

Create an interface called Reader with a Read method.

Go

Final Project

Create a simple program. Use everything you learned.

type Calculator struct {
    history []string
}

func (c *Calculator) Add(a, b int) int {
    result := a + b
    c.history = append(c.history, fmt.Sprintf("%d + %d = %d", a, b, result))
    return result
}

func (c *Calculator) ShowHistory() {
    for _, entry := range c.history {
        fmt.Println(entry)
    }
}

func main() {
    calc := &Calculator{}
    calc.Add(5, 3)
    calc.Add(10, 20)
    calc.ShowHistory()
}

Exercise

Create your own calculator with multiply and divide methods.

Go

Conclusion

You learned Go. Variables. Functions. Structs. Loops. Conditions.

Simple. Clear. Direct.

Go is powerful. You are ready.


Stay connected

Get notified when we launch courses.