Renátó Bogár
Renato's blog

Renato's blog

Primitive data types in Go

Primitive data types in Go

Renátó Bogár's photo
Renátó Bogár
·May 9, 2022·

3 min read

Subscribe to my newsletter and never miss my upcoming articles

Table of contents

  • Value
  • Pointer
  • Constant
  • Package level constants -> iota
  • Iota

In my first article I mentioned that I think its worth to learn Go and I wrote some important features that Go provides for you. So this time I would like to dig deeper in Go and show the primitive data types in th elanguage.

Value

Value types - so when we said variable i and that was equaled to integer 1, the variable is actually pointing at the value of 42 in memory

func main() {
    /* Primitive data types in go */
    var i int 
    i = 2
    fmt.Println(i)

    var f float32 = 3.14
    fmt.Println(f)

    firstName := "XyZ"
    fmt.Println(firstName)

    b := true
    fmt.Println(b)

    c := complex(1,1)
    fmt.Println(c)

    r,im := real(c), imag(c)
    fmt.Println(r,im)
}

Pointer

We also have a commonly used data type in Go which is called pointer data type. So instead of holding the value directly in the variable, we actually going to hold the address of a location in memory that holds the variable for us, these are pointer data types. Their variable is going to point to another location that holds the information we are interested in.

func main() {
    /* Pointer operator */
    var firstName *string = new(string)

    /* Dereference operator */
    *firstName = "Smth"

    /* Address of operator  */
    fmt.Println(&firstName)

    ptr := &firstName
    fmt.Println(ptr, **ptr)
}

Constant

In a lot of ways constants work the same way as variables do in go. The difference is that a variable can, but a constant can not change its value over time

func main() {
    const pi = 3.14
    fmt.Println(pi)
}

One thing to mention is when we declare a constant, we have to initialize them, because we can not change its value. One another thing in connection with constants is that they constants have to be determined compile time. This means if you would like to assign a functions return value as a constant, you can not, because a functions return value is determined during run time. One another thing to mention here is that the following code will execute

func main() {
    const num = 3
    fmt.Println(num + 1)

    fmt.Println(num + 1.2)
}

This code works. How? It works because we havent explicitly say the type of num variable. If we would like to make its type to an integer, we would have to manually type cast the num value to float32, if we would like to add another float number to it, so it would be like this

fmt.Println(float32(num)+ 1.2)

Package level constants -> iota

package main

import (
    "fmt"
)

const (
    first = 1
    second = "second"
)

func main() {
    fmt.Println(first,second)
}

Iota

constants dynamically generated at compile time

package main

import (
    "fmt"
)

const (
    first = iota
    second = iota
)

func main() {
    /* Every time an iota runs, it increments its value by one */ 
    fmt.Println(first,second)
}

iota resets between the constants block

package main

import (
    "fmt"
)

const (
    first = iota
    second = iota
)

const (
    third = iota
)

func main() {
    /* Every time an iota runs, it increments its value by one, but iotas value resets in a new const block */ 
    fmt.Println(first,second)
}

Did you find this article valuable?

Support Renátó Bogár by becoming a sponsor. Any amount is appreciated!

See recent sponsors Learn more about Hashnode Sponsors
 
Share this