Wednesday 1 May 2019

Go: Primitive Data Types


Below table summarizes the data types provided by Go Language.

Integer Data Types
Data Type
Description
Minimum Value
Maximum Value
uint8

Unsigned 8-bit integers
0
255
uint16

Unsigned 16-bit integers
0
65535
uint32


Unsigned 32-bit integers
0
4294967295
uint64


Unsigned 64-bit integers
0
18446744073709551615
int8

Signed 8-bit integers
-128
127
int16

Signed 16-bit integers
-32768
32767
int32
Signed 32-bit integers
-2147483648
2147483647
int64
Signed 64-bit integers
-9223372036854775808
9223372036854775807

Below table summarizes floating point numbers.

Data Type
Description
float32
IEEE-754 32-bit floating-point numbers
float64
IEEE-754 64-bit floating-point numbers

Below table summarizes the complex numbers.

Data Type
Description
complex64
Complex numbers with float32 real and imaginary parts
complex128
Complex numbers with float64 real and imaginary parts

Apart from the above types, Go language support below types that are implementation specific.

a.   Byte
b.   rune (same as int32)
c.    uint
d.   int
e.   uintptr

Syntax to create variable
var variableName dataType
var variableName dataType  = value
variableName := value

HelloWorld.go
package main

import "fmt"

func main() {
 var a int = 10
 var b uint8 = 11
 var c uint16 = 12
 var d uint32 = 13
 var e uint64 = 14
 var f int8 = 15
 var g int16 = 16
 var h int32 = 17
 var i int64 = 18

 var k float32 = 2
 var l float64 = 2

 complex1 := complex(10, 13)

 fmt.Println("a : ", a);
 fmt.Println("b : ", b);
 fmt.Println("c : ", c);
 fmt.Println("d : ", d);
 fmt.Println("e : ", e);
 fmt.Println("f : ", f);
 fmt.Println("g : ", g);
 fmt.Println("h : ", h);
 fmt.Println("i : ", i);

 fmt.Println("k : ", k);
 fmt.Println("l : ", l);

 fmt.Println("complex1 : ", complex1);

}


Output
a :  10
b :  11
c :  12
d :  13
e :  14
f :  15
g :  16
h :  17
i :  18
k :  2
l :  2
complex1 :  (10+13i)

How to access real and imaginary numbers from complex numbers?
You can use ‘real’ and ‘img’ methods to access the real and complex parts of a number.

myComplex := complex(10, 13)
real(myComplex)
imag(myComplex)


HelloWorld.go
package main

import "fmt"

func main() {

 complex1 := complex(10, 13)
 var realPart = real(complex1)
 var imgPart = imag(complex1)

 fmt.Println("complex1 : ", complex1);
 fmt.Println("realPart : ", realPart);
 fmt.Println("imgPart : ", imgPart);

}


Output

complex1 :  (10+13i)
realPart :  10
imgPart :  13


Previous                                                 Next                                                 Home

No comments:

Post a Comment