Detail DataTypes In Golang you should know 2019

In Detail DataTypes in Golang By Sagar Jaybhay



The data types specify the data which is valid Go Variable. In Go programming language data types divided into 4 different categories.

Basic Data Type: basic data types are categorized into 3 different types

  • Numbers
  • Booleans
  • String

Number DataType in Go Language

DATA TYPE DESCRIPTION
int8 8-bit signed integer
int16 16-bit signed integer
int32 32-bit signed integer
int64 64-bit signed integer
uint8 8-bit unsigned integer
uint16 16-bit unsigned integer
uint32 32-bit unsigned integer
uint64 64-bit unsigned integer
int Both in and uint contain the same size, either 32 or 64 bit.
uint Both in and uint contain the same size, either 32 or 64 bit.
rune It is a synonym of int32 and also represent Unicode code points.
byte It is a synonym of int8.
uintptr It is an unsigned integer type. Its width is not defined, but it can hold all the bits of a pointer value.

Floating Point Number

This is used to store numbers with a decimal component. Go language has 2 types of floating-point which are float32 and float64.

The default values of floating point s float64. What is means? When you initialize variable with without specifying type then the variable is referred to as float64.

var a=3214.567

Operations on Numeric Types

Go provides several operators for performing operations on numeric types.

  • Arithmetic Operators: +, -, *, /, %
  • Comparison Operators: ==, !=, <, >, <=, >=
  • Bitwise Operators: &, |, ^, <<, >>
  • Increment and Decrement Operators: ++, —
  • Assignment Operators: +=, -=, *=, /=, %=, <<=, >>=, &=, |=, ^=

Booleans in Golang

Go language provides bool data type which stores a Boolean value and it has only 2 different values true and false.

Operations on Boolean data types

  • && (logical and)
  • || (logical or)
  • !     (negation)

Complex numbers

The complex numbers are one of the basic data types. It also has 2 different sizes complex64 and complex128

Complex64- has real and imaginary part of float32

Complex128: has a real and imaginary part of float64

var v= 4+7i;

If you want to create a complex number then go language has a built-in complex function for creating a complex number.

var a = 10.11
var b = 22.33

// var c = a + bi won't work. Create a complex number like this -
var c = complex(a, b)

both real and imaginary parts of complex numbers must be the same floating-point type.

But if you try to create a complex number with different real and imaginary part types then it will throw an error.

Operations on complex numbers:  it is addition, subtraction, multiplication, and division.

Strings datatype in golang

It is a sequence of bytes. We can declare a string with double-quotes. If string enclosed in double-quotes then they can have escape character like \n, \t. this escaped character replaced with newline and tab space.

If you enclosed string with backtick then this string treated as a raw string.

Datatypes in golang
Datatypes in golang

Number.go file

package utilitypackage

import (
    "fmt"
    "reflect"
)

var Name string = "sagar"

func CreateDataTypes() {
    a := 10
    b := true
    c := 12.144
    d := "Sagar Jaybhay"
    e := complex(3.7, 4.8)

    fmt.Println(reflect.TypeOf(b), b)
    fmt.Println(reflect.TypeOf(a), a)
    fmt.Println(reflect.TypeOf(c), c)
    fmt.Println(reflect.TypeOf(d), d)

    fmt.Println(reflect.TypeOf(e), e)
}

Main.go file

package main

import (
    "fmt"

    "./utilitypackage"
)

func main() {

    //fmt.Println(utilitypackage.Reverse("sagar"))
    utilitypackage.CreateDataTypes()
    
    fmt.Println(utilitypackage.Name)

}

Point to remember whenever you want to access something outside of file your First letter of variable or function should be capital.

Formating the value:

The package fmt implements formatted I/ O.

Refer link https://golang.org/pkg/fmt/

For example:

%v	the value in a default format
	when printing structs, the plus flag (%+v) adds field names
%#v	a Go-syntax representation of the value
%T	a Go-syntax representation of the type of value
%%	a literal percent sign; consumes no value

Boolean

%t	the word true or false

Integer

%b	base 2
%c	the character represented by the corresponding Unicode code point
%d	base 10
%o	base 8
%O	base 8 with 0o prefix
%q	a single-quoted character literal safely escaped with Go syntax.
%x	base 16, with lower-case letters for a-f
%X	base 16, with upper-case letters for A-F
%U	Unicode format: U+1234; same as "U+%04X"

Floating-point and complex constituents:

%b	decimal less scientific notation with exponent a power of two,
	in the manner of strconv.FormatFloat with the 'b' format,
	e.g. -123456p-78
%e	scientific notation, e.g. -1.234456e+78
%E	scientific notation, e.g. -1.234456E+78
%f	decimal point but no exponent, e.g. 123.456
%F	synonym for %f
%g	%e for large exponents, %f otherwise. The precision is discussed below.
%G	%E for large exponents, %F otherwise
%x	hexadecimal notation (with decimal power of two exponent), e.g. -0x1.23abcp+20
%X	upper-case hexadecimal notation, e.g. -0X1.23ABCP+20

String and slice of bytes (treated equivalently with these verbs):

%s	the uninterpreted bytes of the string or slice
%q	a double-quoted string safely escaped with Go syntax
%x	base 16, lower-case, two characters per byte
%X	base 16, upper-case, two characters per byte

Slice

%p	address of 0th element in base 16 notation, with leading 0x

Pointer

%p	base 16 notation, with leading 0x
The %b, %d, %o, %x and %X verbs also work with pointers,
formatting the value exactly as if it were an integer.

The default format for %v is:

bool:                    %t
int, int8 etc.:          %d
uint, uint8 etc.:        %d, %#x if printed with %#v
float32, complex64, etc: %g
string:                  %s
chan:                    %p
pointer:                 %p

For compound objects, the elements are printed using these rules, recursively, laid out like this:

struct:             {field0 field1 ...}
array, slice:       [elem0 elem1 ...]
maps:               map[key1:value1 key2:value2 ...]
pointer to above:   &{}, &[], &map[]

 Formating in golang
Formating in golang

Number.go file

package utilitypackage

import (
    "fmt"
)

// this name field is used in the main package
var Name string = "sagar"

// This CreateDataTypes called in main function
func CreateDataTypes() {
    a := 10
    b := true
    c := 12.144
    d := "Sagar Jaybhay"
    e := complex(3.7, 4.8)

    fmt.Printf("%v \t", a)
    fmt.Printf("%v \t ", b)
    fmt.Printf("%v \t", c)
    fmt.Printf("%v \t", d)
    fmt.Printf("%v \t", e)

    fmt.Println("\n-------------------------------------------------")

    fmt.Printf("%T \t", a)
    fmt.Printf("%T \t ", b)
    fmt.Printf("%T \t", c)
    fmt.Printf("%T \t", d)
    fmt.Printf("%T \t", e)
}

Main.go file

package main

import (
    "fmt"

    "./utilitypackage"
)

func main() {

    //fmt.Println(utilitypackage.Reverse("sagar"))
    utilitypackage.CreateDataTypes()

    fmt.Println("\n called in main ", utilitypackage.Name)

}

GitHubg Project Link :- https://github.com/Sagar-Jaybhay/GoLanguageLab

Sagar Jaybhay, from Maharashtra, India, is currently a Senior Software Developer at Software Company. He has continuously grown in the roles that he has held in the more than seven years he has been with this company. Sagar Jaybhay is an excellent team member and prides himself on his work contributions to his team and company as a whole.

Sagar Jaybhay

Sagar Jaybhay, from Maharashtra, India, is currently a Senior Software Developer at Software Company. He has continuously grown in the roles that he has held in the more than seven years he has been with this company. Sagar Jaybhay is an excellent team member and prides himself on his work contributions to his team and company as a whole.

You may also like...