Mixed Variable Declaration in Go

Watch out! This tutorial is over 5 years old. Please keep this in mind as some code snippets provided may no longer work or need modification to work on current systems.
Tutorial Difficulty Level    

Go variables of different types can be declared in one go using type inference.

package main

import "fmt"

func main() {
   var a, b, c = 3, 4, "foo"  
	
   fmt.Println(a)
   fmt.Println(b)
   fmt.Println(c)
   fmt.Printf("a is of type %T\n", a)
   fmt.Printf("b is of type %T\n", b)
   fmt.Printf("c is of type %T\n", c)
}

When the above code is compiled and executed, it produces the following result:

3
4
foo
a is of type int
b is of type int
c is of type string

 

CategoriesGo