Sling Academy
Home/Golang/Multiple ways to create a slice in Go

Multiple ways to create a slice in Go

Last updated: November 20, 2024

Slices are a powerful and flexible feature in the Go programming language, designed to work with sequences of data. Let's explore various ways to create and manipulate slices in Go, starting from basic techniques and moving on to more advanced practices.

Basic Usage of Slices

The simplest way to create a slice is to declare it using a literal:

package main

import "fmt"

func main() {
    // Create a slice with pre-defined values using a slice literal
    numbers := []int{1, 2, 3, 4, 5}
    fmt.Println(numbers)
}

Using the make Function

Slices can also be created using the make function, which allows you to define the length and optional capacity:

package main

import "fmt"

func main() {
    // Create a slice with a specified length and capacity
    numbers := make([]int, 3, 5)
    fmt.Println(numbers) // Output: [0 0 0]
}

Sub-slicing

You can create a new slice by slicing an existing slice:

Consider a slice called numbers:

numbers := []int{1, 2, 3, 4, 5}
subSlice := numbers[1:4]
fmt.Println(subSlice) // Output: [2 3 4]

Intermediate Slice Operations

Let's look at some more concepts using slices.

Appending to a Slice

package main

import "fmt"

func main() {
    var numbers []int
    numbers = append(numbers, 1, 2, 3)
    fmt.Println(numbers) // Output: [1 2 3]
}

Appending a slice to another slice:

additionalNumbers := []int{4, 5, 6}
numbers = append(numbers, additionalNumbers...)
fmt.Println(numbers) // Output: [1 2 3 4 5 6]

Advanced Techniques

For advanced game play with slices, consider how to manipulate slice capacities and reference behavior.

Changing Capacity

Because slices are references to arrays, quote/unquote capacity changes when you append enough elements.

package main

import "fmt"

func main() {
    // Creating a slice with no initial values
    numbers := make([]int, 0, 3)
    fmt.Printf("Initial Capacity: %d\n", cap(numbers))

    // Append exceeding the initial capacity
    numbers = append(numbers, 1, 2, 3, 4)
    fmt.Printf("New Capacity: %d\n", cap(numbers))
}

Performance Considerations

When working with slices, understanding the difference between length and capacity, and how append operates to create a new underlying array when capacity is exceeded, is crucial to writing efficient code.

Remember always to use slice copying with caution:

package main

import "fmt"

func main() {
    originalSlice := []int{1, 2, 3, 4, 5}
    copiedSlice := make([]int, len(originalSlice))
    copy(copiedSlice, originalSlice)
    fmt.Println(copiedSlice) // Output: [1 2 3 4 5]
}

Conclusion

Slices in Go provide a robust and efficient way to work with collections of data. Whether you are appending elements, modifying them, or slicing them into new slices, Go's slices make it simple and efficient.

Next Article: How to get the length of a slice in Go

Previous Article: Array vs Slice in Go: What is difference?

Series: Working with Slices in Go

Golang

Related Articles

You May Also Like

  • How to remove HTML tags in a string in Go
  • How to remove special characters in a string in Go
  • How to remove consecutive whitespace in a string in Go
  • How to count words and characters in a string in Go
  • Relative imports in Go: Tutorial & Examples
  • How to run Python code with Go
  • How to generate slug from title in Go
  • How to create an XML sitemap in Go
  • How to redirect in Go (301, 302, etc)
  • Using Go with MongoDB: CRUD example
  • Auto deploy Go apps with CI/ CD and GitHub Actions
  • Fixing Go error: method redeclared with different receiver type
  • Fixing Go error: copy argument must have slice type
  • Fixing Go error: attempted to use nil slice
  • Fixing Go error: assignment to constant variable
  • Fixing Go error: cannot compare X (type Y) with Z (type W)
  • Fixing Go error: method has pointer receiver, not called with pointer
  • Fixing Go error: assignment mismatch: X variables but Y values
  • Fixing Go error: array index must be non-negative integer constant