Complete the code to sort the slice of integers in ascending order.
package main import ( "fmt" "sort" ) func main() { numbers := []int{5, 3, 8, 1, 2} sort.[1](numbers) fmt.Println(numbers) }
The sort.Ints function sorts a slice of integers in ascending order.
Complete the code to check if the slice is sorted in ascending order.
package main import ( "fmt" "sort" ) func main() { numbers := []int{1, 2, 3, 4, 5} isSorted := sort.[1](sort.IntSlice(numbers)) fmt.Println(isSorted) }
The sort.IsSorted function returns true if the slice is sorted in ascending order.
Fix the error in the code to sort a slice of strings alphabetically.
package main import ( "fmt" "sort" ) func main() { words := []string{"banana", "apple", "cherry"} sort.[1](words) fmt.Println(words) }
The sort.Strings function sorts a slice of strings alphabetically.
Fill both blanks to create a map of words to their lengths for words longer than 3 characters.
package main import "fmt" func main() { words := []string{"go", "code", "sort", "data"} lengths := make(map[string]int) for _, word := range words { if len(word) [1] 3 { lengths[word] = [2] } } fmt.Println(lengths) }
The condition len(word) > 3 filters words longer than 3 characters, and len(word) gives the length value for the map.
Fill all three blanks to create a map of uppercase words to their lengths for words longer than 2 characters.
package main import ( "fmt" "strings" ) func main() { words := []string{"go", "code", "sort", "data"} lengths := make(map[string]int) for _, word := range words { if len(word) [1] 2 { lengths[strings.[2](word)] = [3] } } fmt.Println(lengths) }
The condition len(word) > 2 filters words longer than 2 characters. strings.ToUpper(word) converts words to uppercase keys, and len(word) is the map value.