DEV Community

Cover image for Go lang Basic
Sabbir Alam
Sabbir Alam

Posted on

Go lang Basic

Data type in Go lang

  1. int
  • int
  • int8
  • int16
  • int32
  • int64
  1. uint
  • uint
  • uint8
  • uint16
  • uint32
  • uint64
  1. float
  • float32
  • float64
  1. string
  2. slice
  3. array
  4. map
  5. struct
  6. Interface{} 10.Pointer 11.you can assign only a positive value in an unsigned integer

The default value of Data Type

  • The default value of int is 0.
  • The default value of float is 0.0.
  • The default value of a string is “”.
  • The default value of a slice is nil.
  • The default value of the array depends on the assigned data type.
  • The default value of a map is nil.
  • The default value of a struct is nil.
  • The default value of an interface is nil.
  • The default value of a pointer is nil.

Explanation of Data Types

Slice

  • slice does not define the number of items
  • The default value of a slice is nil.
  • Slice has capacity & length.
  • We can append the slice data.
  • we can access the slice like an array.
  • we can assign unlimited data in a slice
package main
import "fmt"
func main() {
    a := []int{1,3,4,5}
    res := sum(a)
    fmt.Println("slice: ", res)
    res = multi(a)
    fmt.Println("slice: ", res)
}

func sum(number []int) int {
    total := 0
    for _, num:= range number {
        total += num
    }
    return total
}
func multi(number []int) int {
    total := 1
    for _, num:= range number {
        total *= num
    }
    return total
}
Enter fullscreen mode Exit fullscreen mode

Array

  • The number of data is defined in an array.
  • The default value of the array depends on the assigned data type.
  • The array has only length and doesn't have capacity.
  • We can't append an array, we can index an array
  • The size of the array is also called, more data cannot be assigned.
  • We can index keys and values in an array.
  • If we have to check the length of the array, we can use the default built-in function len() and print the required data in the terminal or web.
package main
import "fmt"
func main() {
    a := [5]int{}
    fmt.Println("integer Array: ", a)

    var b = [4]string{
        "codemen",
        "m",
        "codemen",
        "codemen",
    }
    fmt.Println("Name: ", b)

    colors := [4]string{
        "roje","gris","azul","black"}

        fmt.Println("Name: ", colors)
}
Enter fullscreen mode Exit fullscreen mode

Map

  • The default key of a map could be an integer or a string. The value could be any type of data like integer, string, slice, array, or map.
package main

import "fmt"

type Vertex struct {
    Lat, Long float64
}

var m map[string]Vertex
func main() {
    m = make(map[string]Vertex)
    m["Bell Labs"] = Vertex{
        40.68433, -74.39967,
    }
    fmt.Println(m["Bell Labs"])
}

var a string = "codemen"
var m map[string]string
func main() {
    m = make(map[string]string)
    m["Hello String"] = a
    fmt.Println(m["Hello String"])
}
Enter fullscreen mode Exit fullscreen mode

Struct

  • Struct users define data type.

Interface

  • An interface can use for dynamic purposes. We can assign any kind of data in an interface.
  • We can declare/signature a method in an interface. We can't implement it in an interface.
  • In an interface, we can define methods type, arguments, and return type.

Pointer

  • A pointer holds the address of a variable
  • A pointer holds its memory address of Ram.
  • To dynamically change the variable value we can use a pointer
  • If we want to change one of the variable's values under the block, function, for loop, if conditions, then we can use a pointer to hold the variable's value and change it through it.

Top comments (0)