Identify Variable type and values in go lang
package main
import "fmt"
func main() {
var name string = "Mahesh"
age := 25
price := 300.40
fmt.Printf("%T %v\n",name,name)
fmt.Printf("%T %v\n",age,age)
fmt.Printf("%T %v\n",price,price)
}
Output
string Maheshint 25
float64 300.4
Explanation
- In the above program snippet, we use Printf() function of fmt package to print the formatted output.
- Withing the Printf( ) %T and %v is called format specifier which represents the output format.
- %T represents the Type, which prints the variable type
- %v represents the variable's value.
- age := 25, creates a variable age with the short variable declaration and assign the value 25. In this case, the variable automatically receives the type and here age is the type of int.
- price:=300.40, In this statement price, is created of type float64 because float64 is the default type of floating values in go lang. To create a float32 type variable you need to create a variable as given below.
- \n is called escape sequence which changes the line.
- Escape sequences are some not printable characters that handle specially and starts with a backslash (\). \n called newline which changes the line and \t is called tab which prints the tab equivalent space.
var price float32 = 300.40
var price float32 = 300.40
Comments