Primitive Data Types in Go

 Variable declarations and primitives


(1) Declaration using var keyword

var <variable name > <type>

var <variable name > <type>
package main

import "fmt"

func main() {
    var i int
    i = 24
    fmt.Println(i)
}

This is used if you want to initialize a variable at a different position

Another way is 

package main

import "fmt"

func main() {
    var number int = 52
    fmt.Println(number)
}


(2) Implicit initialization

Here go with pick up the data type


package main

import "fmt"

func main() {
    number := 52
    fmt.Println(number)
}


All local variables that are declared should be used otherwise it will throw a compilation error.


Data Types

(1) Value data types - Here the variables refer to the values the variables hold

(2) Pointer Data types - Here the variables refer to the address location where the values are stored.


Complex data type

This is a built in data type in Go.

package main

import "fmt"

func main() {
    a := 10.0
    b := 5.0
    c := complex(a, b)
    fmt.Println(c)
    ri := real(c), imag(c)
    fmt.Println(r, i)
}

Output :
(10+5i) 10 5


Type 

int An integer. Holds whole numbers.

float64 A floating-point number. Holds numbers with a fractional part. (The 64 in the type

name is because 64 bits of data are used to hold the number. This means that

float64 values can be fairly, but not infinitely, precise before being rounded off.)

bool A Boolean value. Can only be true or false.

string A string. A series of data that usually represents text characters


No comments:

 Python Basics How to check the version of Python interpreter mac terminal

Popular in last 30 days