The Journey of #100DaysofCode (@eliehannouch)

100daysofcode - Day-02

Hello folks, Here we go, new day Day-02 and some new knowledge gained in Golang. Today we will dive in the GoLang data types.

GoLang and the different data types

  • Go is a statically typed language. Which means that variable types are known at compile time

  • Go use type inference where the compiler can infer the type of a certain variable without explicitly specify it by the developers

  • Primitive data types in GoLang

    1. Signed Integer Types (int8, int16, int, int32, int64)

    2. Unsigned Integer Types (uint8, byte, uint16, uint, uint32, uint64, uintptr)

    3. Others (bool, float32, float64, complex64, complex128)

    Rune and Strings in Golang

    • What is a Rune ?
      1. Code points define characters in unicode. In the case of Go there is a new term called rune.
      2. Runes are aliases of type int32
      3. To find a char rune we can simply write the following piece of code:
           str:= 'B'
           fmt.Println(rune(str))
          // Output: 66
  • What is a String ?
    1. String is a data type that represent a slice of bytes that are UTF-8 encoded. Strings are enclosed with double quotes.
      strExample := "Hello everyone, this a string"

    2. We can get the String length by typing the following
      len([]rune(strExample)), or len(strExample)

    3. We can use the rune() function to convert string to an array of runes

    4. Moving from String to rune

            strExample := "Hello GoLang"
            fmt.Println([]rune(strExample))

            Output: [72 101 108 108 111 32 71 111 76 97 110 103]

Type Aliases

  • Type aliasing refers to the technique of providing an alternate name for an existing type, with an aim to facilitate code refactor for large codebases.
    Example:
    - type productID int
    - type productPrice float64
7 Likes