Go Lesson 1. What are Primitive Data Types

Go Lang is Statically and Strongly Typed language. Which means every variable and constant should have a type at compile time.

Primitive Data types: Basic Data Types such as int, string and bool.

Composite Data Types: Made from basic data types they are e.g array of int.

Booleans, strings and numbers

  • Boolean has two states only. It either can be true or false.
  • Strings are used to represent text which can be single or multiline.
  • Go supports unicode strings.
  • Numbers include int, floats and complex numbers.
  • The Concept of Zero values in Go.
package main
 
import "fmt"
 
func main(){
    var myString string
    // string zero value is ""
    fmt.Println(myString)
    mystring = "Welcome to Go"
    fmt.Println(myString)
 
    var myInt int
    // zero value for number type is 0
    fmt.Println(myInt)
    myInt = 10
    fmt.Println(myInt)
 
    var myBool bool
    //zero value for bool is false
    fmt.Println(myBool)
    myBool = true
    fmt.Println(myBool)
}

Save as “data_types.go

run: go data_types.go

output:

Welcome to Go
0
10
false
true

Go Lesson 2. Integers

01 Data type int

  • int data type is used to store positive and negative integers without fractions
  • eg. 1, 100, -2121, 9121.
  • Different kinds of int data types are: int8, int16, int32, etc.
  • Different int data type have different size in memory and different range of numbers it can store.
  • The first bit in int data type is used to store the sign of the value and the rest bit stores the value itself.

Data Type int

TypeDescriptionRange
int8the set of all signed 8-bit integers-128 to 127
int16the set of all signed 16-bit integers-32768 to 32767
int32the set of all signed 32-bit integers-2147483648 to 2147483647
int64the set of all signed 64-bit integers-9223372036854775808 to 9223372036854775807
02 Data Type uint (unsigned int)
  • The uint data type is used to store positive integers only (without fractions) eg. 1, 2349087, 12, 23423545345.
  • Different kinds of uint data types: uint8, unt16, uint32, etc
  • They have different size in memory as well different range of numbers to be stored.
  • all bits in the memory are used to store value as such it has large range and can store large positive numbers.

Data Type uint

TypeDescriptionRange
uint8the set of all unsigned 8-bit integers0-235
uint16the set of all unsigned 16-bit integers0 - 65535
uint32the set of all unsigned 32-bit integers0 - 4294967295
uint64the set of all unsigned 64-bit integers0 - 18446744073709551615

Other Related Data Types

TypeDescription
bytealias for uint8
runealias for int32
uinteither 32 or 64 bits dependant on the system architecture
inteither 32 or 64 bits dependant on the system architecture
main.go
package main 
import "fmt"
 
func main() {
    // Variable of type uint8
    var smalPositiveValue uint8
    smalPositiveValue = 10
    fmt.Println(smalPositiveValue)
    // Variable of type int8
    var smallPositiveNegativeInt int8
    smallPositiveNegativeInt = -10
    fmt.Println(smallPositiveNegativeInt)
    // uint16, uint32, uint64
    // int16, int32, int64
    var myInt int = 2300000043
    fmt.Println(myInt)
    myInt = int(smallPositiveNegativeInt)
    myInt = int(smalPositiveValue)
    // to typecase a value int()
    var myByte byte = 'A'
    fmt.Println(myByte)
    //byte is mainly use to represent raw data
    // and also to distinguish between uint8 and byte
    // since byte is an alias for uint8
 
    // Rune is an alias for int32
    var myRune rune = 'B'
    fmt.Println(myRune)
    myRune = '💡'
    fmt.Println(myRune)
}