Learn / Go / Loops and functions

Go, lesson 3 of 4

Loops and functions

A function lists the type of each input and the type it returns:

func sum(n int) int {
    total := 0
    for i := 1; i <= n; i++ {
        total += i
    }
    return total
}

Go has one loop keyword, for. Call the function from main and print the result.

Your turn

Add up every number from 1 to 100 and print the total (it is 5050).

Press Run to see what your code prints.