Slice Composite Literal in Go
A slice composite literal is a shorthand notation to create a new slice by specifying its elements directly. It’s similar to an array literal, but you don’t provide the length of the slice. The length of the slice is determined by the number of elements you provide within the literal. A slice literal creates a new underlying array and then returns a slice that references that array.
Here’s an example:
package main
import "fmt"
func main() {
// Creating a slice using a slice composite literal
numbers := []int{1, 2, 3, 4, 5}
// Printing the slice
fmt.Println("Numbers:", numbers)
}
Output :
Numbers: [1 2 3 4 5]
In this example, we create a new slice named numbers
using a slice composite literal. The number of elements inside the literal determines the length of the slice, which is 5 in this case.
You can also use a slice composite literal to create a slice that covers a subset of an existing array or slice:
package main
import "fmt"
func main() {
// Creating a slice using a slice composite literal
fruits := []string{"apple", "banana", "orange", "grape"}
// Creating a slice that covers a subset using the slice composite literal
subsetFruits := fruits[1:3]
// Printing the subset slice
fmt.Println("Subset Fruits:", subsetFruits)
}
Output :
Subset Fruits: [banana orange]
In this example, subsetFruits
covers elements from index 1 (inclusive) to index 3 (exclusive) of the fruits
slice.
Slice composite literals provide a succinct way to create slices with initial values without the need to specify the length explicitly.
How to sort a slice of ints in Golang?
In Go, you can use the sort
package to sort slices of built-in types like int
. The sort
package provides various sorting functions that work with slices.
Here’s how you can sort a slice of int
in Go:
package main
import (
"fmt"
"sort"
)
func main() {
// Creating an unsorted slice of integers
numbers := []int{5, 2, 9, 1, 5, 6, 3}
// Sorting the slice in ascending order
sort.Ints(numbers)
// Printing the sorted slice
fmt.Println("Sorted Numbers:", numbers)
}
Output :
Sorted Numbers: [1 2 3 5 5 6 9]
In this example, we first import the sort
package. Then, we create an unsorted slice named numbers
containing integers. We use the sort.Ints()
function to sort the slice in ascending order. The original numbers
slice is modified in place and becomes sorted.
If you need to sort in descending order, you can reverse the slice after sorting:
package main
import (
"fmt"
"sort"
)
func main() {
// Creating an unsorted slice of integers
numbers := []int{5, 2, 9, 1, 5, 6, 3}
// Sorting the slice in ascending order
sort.Ints(numbers)
// Reversing the sorted slice to get descending order
for i, j := 0, len(numbers)-1; i < j; i, j = i+1, j-1 {
numbers[i], numbers[j] = numbers[j], numbers[i]
}
// Printing the sorted slice in descending order
fmt.Println("Sorted Numbers (Descending):", numbers)
}
Output :
Sorted Numbers (Descending): [9 6 5 5 3 2 1]
Remember that the sort
package works only with slices and doesn’t modify the original slice type. It’s a powerful tool for sorting various types of slices in both ascending and descending order.