Go programming – Data type

In the last post we covered the basic installation, and strings now let us see the datatypes of golang. Go’s basic types are

bool
string
int
int8
int16
int32
int64
uint
uint8 or byte
uint16
uint32 or rune
uint64
uintptr
float32
float64
complex64
complex128

Now lets try to print the datatype of some object using reflect package.


package main

import (
 "fmt"
 "reflect"
 "math/cmplx"
)

func main() {
 b := true
 s := ""
 n := 1
 f := 1.0
 a := []string{"foo", "bar", "baz"}
 c := 0xff
 c_1 := 0xffffffffffffffff
 complex := cmplx.Sqrt(-5 + 12i)
 complex_1 := complex64(complex)

 fmt.Println(reflect.TypeOf(b))
 fmt.Println(reflect.TypeOf(s))
 fmt.Println(reflect.TypeOf(n))
 fmt.Println(reflect.TypeOf(f))
 fmt.Println(reflect.TypeOf(a))
 fmt.Println(reflect.TypeOf(c))
 fmt.Println(reflect.TypeOf(c_1))
 fmt.Println(reflect.TypeOf(complex))
 fmt.Println(reflect.TypeOf(complex_1))
}

Output:

bool
string
int
float64
[]string
int
int64
complex128
complex64

In this example I created a bunch of variable of different datatype. I used the reflect package for printing the datatype. The reflect package implements run-time reflection, allowing a program to manipulate objects with arbitrary types. For more information about reflect refer to this reflect reference manual.

You can download the source code from https://github.com/codelectron/GoLangSeries .

Leave a Reply

Your email address will not be published. Required fields are marked *