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:
fmt.Println
.:=
syntaxSee the full source on github.