Algorithm


In Go, a slice is a dynamically-sized, flexible view into the elements of an array. Slices are more powerful than arrays and are often used instead of arrays for their flexibility. Here are some key points about slices in Go:

  1. Declaration: You can declare a slice using the following syntax:

    go
    var mySlice []int
 

Alternatively, you can create a slice using the make function:

go
mySlice := make([]int, 3, 5)
  •  

    This creates a slice with a length of 3 and a capacity of 5.

  • Initialization: You can initialize a slice using a slice literal:

    go
    mySlice := []int{1, 2, 3, 4, 5}
  •  

    This creates a slice with a length of 5 and a capacity equal to its length.

  • Accessing Elements: Elements of a slice can be accessed using square brackets, just like arrays:

    go
    value := mySlice[2] // Access the element at index 2
  • Slice Operations: Slicing is a powerful feature in Go. You can create a new slice by slicing an existing one:

    go
    newSlice := mySlice[1:4] // Creates a new slice from index 1 to 3 (excluding 4)
  • Note that slices share the same underlying array, so modifying elements in the new slice will also affect the original slice.

  • Appending Elements: You can use the append function to add elements to a slice. If the underlying array has insufficient capacity, a new array will be created, and the data will be copied over:

    go
    mySlice = append(mySlice, 6, 7, 8)
  • Length and Capacity: The len function returns the number of elements in a slice, and the cap function returns its capacity:

    go
    length := len(mySlice)
    capacity := cap(mySlice)
  • Nil Slice: A nil slice in Go has a length and capacity of 0 and is often used to represent an uninitialized slice:

    go
    var nilSlice []int
  • Range: You can use the range keyword to iterate over elements in a slice:

    go
    for index, value := range mySlice {
        fmt.Printf("Index: %d, Value: %d\n", index, value)
    }

Slices are a versatile and powerful data structure in Go, providing a flexible way to work with collections of data.

 

Code Examples

#1 Example Golang Slice

Code - Golang Programming

// Program to create a slice and print its elements

package main
import "fmt"

func main() {

  // declare slice variable of type integer
  numbers := []int{1, 2, 3, 4, 5}

  // print the slice
  fmt.Println("Numbers: ", numbers)
}
Copy The Code & Try With Live Editor

Output

x
+
cmd
Numbers: [1 2 3 4 5]

#2 Create Slice from Array in Go Lang Programming

Code - Golang Programming

// Program to create a slice from an array

package main
import "fmt"

func main() {

  // an integer array
  numbers := [8]int{10, 20, 30, 40, 50, 60, 70, 80}

  // create slice from an array
  sliceNumbers := numbers[4 : 7]

  fmt.Println(sliceNumbers)

}
Copy The Code & Try With Live Editor

Output

x
+
cmd
[50, 60, 70]

#3 Adds Element to a Slice GoLang Programing

Code - Golang Programming

// Program to add elements to a slice

package main
import "fmt"

func main() {
  primeNumbers := []int{2, 3}
  
  // add elements 5, 7 to the slice
  primeNumbers = append(primeNumbers, 5, 7)
  fmt.Println("Prime Numbers:", primeNumbers)
}
Copy The Code & Try With Live Editor

Output

x
+
cmd
Prime Numbers: [2 3 5 7]

#4 Code Example with Golang Programming

Code - Golang Programming

// Program to add elements of one slice to another

package main
import "fmt"

func main() {
  
  // create two slices
  evenNumbers := []int{2, 4}
  oddNumbers := []int{1, 3}  
  
  // add elements of oddNumbers to evenNumbers
  evenNumbers = append(evenNumbers, oddNumbers...)
  fmt.Println("Numbers:", evenNumbers)
}
Copy The Code & Try With Live Editor

Output

x
+
cmd
Numbers: [2 4 1 3]

#5 Code Example with Golang Programming

Code - Golang Programming

// Program to copy one slice to another

package main
import "fmt"

func main() {

  // create two slices
  primeNumbers := []int{2, 3, 5, 7}
  numbers := []int{1, 2, 3}

  // copy elements of primeNumbers to numbers
  copy(numbers, primeNumbers)

  // print numbers
  fmt.Println("Numbers:", numbers)
}
Copy The Code & Try With Live Editor

Output

x
+
cmd
Numbers: [2 3 5]

#6 Find the Length of a Slice GoLang Programming

Code - Golang Programming

// Program to find the length of a slice

package main
import "fmt"

func main() {

  // create a slice of numbers
  numbers := []int{1, 5, 8, 0, 3}

  // find the length of the slice
  length := len(numbers)

  fmt.Println("Length:", numbers)

}
Copy The Code & Try With Live Editor

Output

x
+
cmd
Length: 5

#7 Looping through GoLang Slice

Code - Golang Programming

// Program that loops over a slice using for loop

package main
import "fmt"

func main() {
  numbers := []int{2, 4, 6, 8, 10}

  // for loop that iterates through the slice
  for i := 0; i  <  len(numbers); i++ {
    fmt.Println(numbers[i])
  }

}
Copy The Code & Try With Live Editor

Output

x
+
cmd
2
4
6
8
10
Advertisements

Demonstration


Go Lang Programming Slice