Wednesday, 1 May 2019

Go language: Type Inference


When you initialize a variable without specifying type, then the type of the variable is inferred from the value on the right hand side.

App.go
package main

import "fmt"

func main() {
 i := 10
 j := 10.23
 k := true
 l := "Hello World"

 fmt.Printf("i is of type %T\n", i)
 fmt.Printf("j is of type %T\n", j)
 fmt.Printf("k is of type %T\n", k)
 fmt.Printf("l is of type %T\n", l)
}

Output
i is of type int
j is of type float64
k is of type bool
l is of type string


Previous                                                 Next                                                 Home

No comments:

Post a Comment