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 Mahesh
int 25
float64 300.4

Explanation

  1. In the above program snippet, we use Printf() function of fmt package to print the formatted output.
  2. Withing the Printf( ) %T and %v is called format specifier which represents the output format.
  3. %T represents the Type, which prints the variable type
  4. %v represents the variable's value.
  5. 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.
  6. 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. 
  7. \n is called escape sequence which changes the line.
  8. 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

Comments

Popular posts from this blog

String in golang

Arrays in C Language

Literals in Java

Pointers in C Language

Inline V/S Block Level Element

Reserved Words in Java

Identifiers

Data Types in Go language

Printing in C programming

Variable Naming & Scope of Variable