1
0
mirror of https://github.com/jbranchaud/til synced 2026-01-02 22:58:01 +00:00
Files
til/go/create-a-slice-from-an-array.md
2024-12-26 09:47:12 -07:00

1.0 KiB

Create A Slice From An Array

Slices in Go are a flexible abstraction over arrays. We can create a slice from an array with the [n:m] slicing syntax. We specify the left and right (exclusive) bounds of the array that we want to create the slice relative to.

We can exclude the lower bound which translates to the 0 index of the array. We can exclude the left bound which translates to the end of the array. We can even exclude both ends of the slicing syntax which means creating a slice of the entire array.

Here is an example of each of those:

package main

import "fmt"

func main() {
	arr := [...]string{
		"taco",
		"burrito",
		"torta",
		"enchilada",
		"quesadilla",
		"pozole",
	}

	firstTwo := arr[:2]
	lastTwo := arr[len(arr)-2:]
	all := arr[:]

	fmt.Println("First two:", firstTwo)
	// First two: [taco burrito]

	fmt.Println("Last two:", lastTwo)
	// Last two: [quesadilla pozole]

	fmt.Println("All:", all)
	// All: [taco burrito torta enchilada quesadilla pozole
}

source