Exercise 1

Exercise 1: Unused Variables

Create, save and run this file:

package main

import "fmt"

func main() {
        var varInt = 42
        var varString = "mega"
        var varFloat = 3.14
        fmt.Printf("Value Int : %v\n", varInt)
        fmt.Printf("Type Int  : %T\n", varInt)
}

You will get an error:

go run main.go
# command-line-arguments
./main.go:7:6: varString declared but not used
./main.go:8:6: varFloat declared but not used

GO checks, if a variable is unused. And this is strictly enforced, you will not be able to rune the programm!

Usually you should compile a programm before running it, so you can see the warnings before. Your IDE will also warn you:

VSCode:

Source

See the full source on github.

Sources