[Golang 03/30] Go: data types commonly used
Tags: go, golang30days
Published: 2023-05-25 05:38:20

---
title: [Golang 03/30] Go: data types commonly used
published: true
description: Learning Golang 30 days searies
tags: golang, golang30days
cover_image: https://i0.wp.com/meritocracy.is/blog/wp-content/uploads/2021/04/golang.jpg?fit=1280%2C710&ssl=1
---


**Numeric Types:**

- `int`: Signed integer type. 
- `int8, int16, int32, int64`: Signed integer types with specific bit sizes.
- `uint`: Unsigned integer type.
- `uint8, uint16, uint32, uint64`: Unsigned integer types with specific bit sizes.
- `float32, float64`: Floating-point types for representing decimal numbers.
- `complex64, complex128`: Complex number types for representing real and imaginary parts.

Positive and negative integer
> `int` depends on the running system. If you're running Go on 32-bit OS system, the default type for `int` is `int32`， and its range is -2^31 to 2^31-1(-2147483648, 2147483647)

Positive integer
> `unit8` has the range of `(0, 2^8-1)`, i.e. `(0, 255)`

**Boolean Type:**

`bool`: Represents a Boolean value (`true` or `false`).

**String Type:**

`string`: Represents a sequence of characters.

**Array Types:**

`array`: A `fixed-size` collection of elements of the `same type`. The size of an array is determined at compile-time.

**Slice Type:**

`slice`: A flexible, `dynamically-sized` sequence of elements. Slices are built on top of arrays.


```go
package main

import (
	"fmt"
)

func main() {
	var intA int = -12
	fmt.Printf("\nintA: %d", intA)
	//intA: -12

	var intB uint = 12
	fmt.Printf("\nintB: %d", intB)
	//intB: 12

	var floatA float64 = 98.6
	fmt.Printf("\nfloatA: %f", floatA)
	//floatA: 98.600000

	var stringVar string = "hello world!"
	fmt.Printf("\nstringVar: %s", stringVar)
	// stringVar: hello world!

	var boolVar bool = true
	fmt.Printf("\nboolVar: %t\n", boolVar)
	// boolVar: true

	var numbers = []int{0, 1, 2, 3, 4, 5}
	fmt.Printf("Size of 'numbers' is %d\n", len(numbers))
	fmt.Println(numbers)
	// Size of 'numbers' is 6
	// [0 1 2 3 4 5]

	numbers = append(numbers, 6, 7)
	fmt.Printf("Size of 'numbers' is changed to %d\n", len(numbers))
	fmt.Println(numbers)
	// Size of 'numbers' is changed to 8
	// [0 1 2 3 4 5 6 7]

	sliced := numbers[1:5]
	fmt.Printf("sliced numbers: ")
	fmt.Println(sliced)
	// sliced numbers: [1 2 3 4]

	numberArray := [3]int{1, 2}
	fmt.Printf("numberArray with size 3 and default value:")
	fmt.Println(numberArray)
	// numberArray with size 3 and default value:[1 2 0]


}
```


**Map Type:**

`map`: An unordered collection of key-value pairs, where each `key is unique`.

```go
package main

import "fmt"

func main() {
    // Create a map to store employee names and their corresponding IDs
	// key: string, value: int
    employees := make(map[string]int)
	fmt.Printf("current size of this map: %d\n", len(employees))
	// current size of this map: 0

    // Add key-value pairs to the map
    employees["John"] = 1001
    employees["Alice"] = 1002
    employees["Bob"] = 1003
    fmt.Println(employees)
	// map[Alice:1002 Bob:1003 John:1001]

    // Accessing values from the map
    fmt.Println(employees["Alice"])  
	// Output: 1002

    // Modifying values in the map
    employees["Bob"] = 1004

    // Deleting a key-value pair from the map
    delete(employees, "John")

    // Print the updated map
    fmt.Println(employees)
	// map[Alice:1002 Bob:1004]

    // Checking if a key exists in the map
    id, exists := employees["Alice"]
    if exists {
        fmt.Println("Alice's ID:", id)
		// Alice's ID: 1002
    } else {
        fmt.Println("Alice not found")
    }

    var keys []string
    for key :=range employees {
      keys = append(keys, key)
    }
	fmt.Printf("Keys from the map 'employees': ")
	fmt.Println(keys)
	// Keys from the map 'employees': [Alice Bob]
}

```