Exercise 2

Exercise 2: Variables Types

Create, save and run this file:

package main

import "fmt"

func main() {

	// var declares a variable
	// and sets the type
	var varInt = 42
	var varString = "mega"
	var varFloat = 3.14

	// fmt "%v" just print almost everything
	// fmt "%T" prints the type
	fmt.Printf("Value Int : %v\n", varInt)
	fmt.Printf("Type Int  : %T\n", varInt)
	fmt.Printf("Type String  : %T\n", varString)
	fmt.Printf("Type Float  : %T\n", varFloat)

}

You will get no “unused” error, because you use all declared variables.

The program will print the value of the variable with the format option %v and the type of the variable with the format option %T. With fmt.Println you can just print the value.

Tasks:

  • Change the program so that it uses fmt.Println.
  • Change the variable declaration to the := syntax

See also

Source

See the full source on github.

Sources