Slice in Go Language

  • date 27th July, 2022 |
  • by Prwatech |
  • 0 Comments

ex 1.

package main

import "fmt"

func main() {
    var x [6]int = [6]int {1, 2, 3, 4, 5,7}
    var a []int = x[1:3]
    fmt.Println(a)
    fmt.Println(len(a))
    fmt.Println(cap(a))
}

ex 2.

package main
import "fmt"

func main() {
    var thing = []string{"HI", "This Is Prwatech  "}
    fmt.Println(thing)
    things = append(thing, "!!!")
    fmt.Println(thing)
}

ex 3.

package main

import "fmt"

func main() {
    var things = []string{"HI", " This  is a Prwatech Institute of Data Scientist "}
    fmt.Println(things)
    subject = append(things, "Analyst ")
    fmt.Println(subject)
    things = append(subject [1 : len(subject)-1])
    fmt.Println(subject)
}

ex 4.

package main
import "fmt"

func main() {
    Names: = [4]string{
        "Python", "Machine Learning", "Deep Learning", "Java",
    }
    fmt.Println(names)
    a: = Names[0:2]
    b: = Names[1:3]
    fmt.Println(a, b)
    b[0] = "***"
    fmt.Println(a, b)
    fmt.Println(Names)
}

ex 5.

package main
import "fmt"
func main() {
    k: = []int {2, 4, 3, 5, 6, 78}
    fmt.Println(k)
    m: = []bool {true, false, true, false, false, true}
    fmt.Println(m)
    t: = []struct {
        i int
        b bool
    }{
        {2, true}, 
{4, false},
        {3, true},
        {5, true},
 {6, false},
        {78, true},
    }
    fmt.Println(t)
}

ex 6.

package main
import "fmt"
func main() {
    s: = []int{2, 3, 4, 5, 6, 7, 8, 9}
    s = s[1:4]
    fmt.Println(s)
    s = s[:2]
    fmt.Println(s)
    s = s[1:]
    fmt.Println(s)
}

ex 7.

package main
import "fmt"
func main() {
    s: = []int{2, 3, 4, 5, 6, 7, 11, 14}
    PrintSlice(s)
    s = s[1:4]
    s = s[:0]
    PrintSlice(s)
    s = s[:4]
    PrintSlice(s)
    s = s[2:]
    PrintSlice(s)
}
func PrintSlice(s []int) {
    fmt.Printf("len=%d cap=%d %v \n", len(s), cap(s), s)
}

Golang Maps

ex 1.

package main
import "fmt"
func main() {
    // Creating and initializing an empty map
    var map_1 map[int]int
    // Checking if the map is nil or not
    if map_1 == nil 
        fmt.Println("True")
    } else {
        fmt.Println("False")
    }
// Creating and initializing a map
    map_2: = map[int]string {
        90: "Math",
        91: "Statistic",
        92: "Python",
        93: "SQL",
        94: "Machine Learning ",
    }
    fmt.Println("Map-2: ", map_2)
}

ex 2.

package main
import "fmt"
// Main function
func main() {
// Creating and initializing a map
    M_a_p: = map[int]string {
        90: "Math",
        91: "Statistic",
        92: "Python",
        93: "SQL",
        94: " Machine Learning ",
    }
    // Iterating map using for rang loop
    for id, pet := range m_a_p {
        fmt.Println(id, pet)
    }
}
  1. Using the Make Function

ex1.

package main

import "fmt"

func main() {

    // Creating a map
    // Using make() function
    var My_course = make(map[float64]string)
    fmt.Println(My_map)

    My_course[1.3] = "Data Scientist"
    My_course[1.5] = "Data Analyst"
    fmt.Println(My_course)
}

ex2.

package main

import "fmt"

func main() {

    Sub: = map[int]string {

        90: "Math",
        91: "Statistic",
        92: "Python",
        93: "SQL",
        94: " Machine Learning ",
    }

    for id, pess: = range Sub{

        fmt.Println(id, pass)
    }
}

 

Quick Support

image image