bugl
bugl
HomeLearnPatternsPathsSearchPremium
HomeLearnPatternsPaths

Loading lesson path

Learn/Go/Go Tutorial
Go•Go Tutorial

Go Access, Change, Append and Copy Slices

Access Elements of a Slice

You can access a specific slice element by referring to the index number.

In Go, indexes start at 0. That means that [0] is the first element, [1] is the second element, etc.

Example

package main
import ("fmt")
func main() {
  prices := []int{10,20,30}
  fmt.Println(prices[0])
  fmt.Println(prices[2])
}

Change Elements of a Slice

You can also change a specific slice element by referring to the index number.

Example

package main
import ("fmt")
func main() {
  prices := []int{10,20,30}
  prices[2] = 50
  fmt.Println(prices[0])
  fmt.Println(prices[2])
}

Append Elements To a Slice

You can append elements to the end of a slice using the append() function:

Syntax

slice_name = append( slice_name , element1 , element2 , ...)

Example

package main
import ("fmt")
func main() {
  myslice1 := []int{1, 2, 3, 4, 5, 6}
  fmt.Printf("myslice1 = %v\n", myslice1)
  fmt.Printf("length = %d\n", len(myslice1))
  fmt.Printf("capacity = %d\n", cap(myslice1))
  myslice1 = append(myslice1, 20, 21)
  fmt.Printf("myslice1 = %v\n", myslice1)
  fmt.Printf("length = %d\n", len(myslice1))
  fmt.Printf("capacity = %d\n", cap(myslice1))
}

Append One Slice To Another Slice

To append all the elements of one slice to another slice, use the append() function:

Syntax

slice3 = append( slice1 , slice2 ...)

Note

The '...' after slice2 is necessary when appending the elements of one slice to another.

Example

package main
import ("fmt")
func main() {
  myslice1 := []int{1,2,3}
  myslice2 := []int{4,5,6}
  myslice3 := append(myslice1, myslice2...)
  fmt.Printf("myslice3=%v\n", myslice3)
  fmt.Printf("length=%d\n", len(myslice3))
  fmt.Printf("capacity=%d\n", cap(myslice3))
}

Change The Length of a Slice

Unlike arrays, it is possible to change the length of a slice.

Example

package main
import ("fmt")
func main() {
  arr1 := [6]int{9, 10, 11, 12, 13, 14} // An array
  myslice1
  := arr1[1:5] // Slice array
  fmt.Printf("myslice1 = %v\n", myslice1)
  fmt.Printf("length = %d\n", len(myslice1))
  fmt.Printf("capacity = %d\n", cap(myslice1))
  myslice1 = arr1[1:3] // Change length by re-slicing the array
  fmt.Printf("myslice1 = %v\n", myslice1)
  fmt.Printf("length = %d\n", len(myslice1))
  fmt.Printf("capacity = %d\n", cap(myslice1))
  myslice1 = append(myslice1, 20, 21, 22, 23) // Change length by appending items
  fmt.Printf("myslice1 = %v\n", myslice1)
  fmt.Printf("length = %d\n", len(myslice1))
  fmt.Printf("capacity = %d\n", cap(myslice1))
}

Memory Efficiency

When using slices, Go loads all the underlying elements into the memory.

If the array is large and you need only a few elements, it is better to copy those elements using the copy() function.

The copy() function creates a new underlying array with only the required elements for the slice. This will reduce the memory used for the program.

Syntax

copy( dest , src )

The copy() function takes in two slices dest and src , and copies data from src to dest . It returns the number of elements copied.

copy()

The capacity of the new slice is now less than the capacity of the original slice because the new underlying array is smaller.

Previous

Go Slices

Next

Go Operators