From f3aa49cea6fc5cd9f500679fd0ccdc8b81a2f6b7 Mon Sep 17 00:00:00 2001 From: Boukabouya Date: Sat, 30 Dec 2023 14:44:13 +0100 Subject: [PATCH] Manage ordered values in slices --- hello/main.go | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/hello/main.go b/hello/main.go index 8ab932f..6aac938 100644 --- a/hello/main.go +++ b/hello/main.go @@ -2,8 +2,28 @@ package main import ( "fmt" + "sort" ) func main() { - fmt.Println("Hello from Go!") + var colors = []string{"Green", "red", "blue"} // now [] it's a slice , i can add new items + fmt.Println(colors) + colors = append(colors, "Purple") + fmt.Println(colors) + colors = append(colors[:len(colors)-1]) + fmt.Print(colors, "\n") + + numbers := make([]int, 5) //([]int, 5(number of items) , 5 (capacity that caps the number of items)) + numbers[0] = 134 + numbers[1] = 72 + numbers[2] = 32 + numbers[3] = 12 + numbers[4] = 156 + fmt.Println(numbers) + numbers = append(numbers, 235) + fmt.Println(numbers) + // sort slice by adding the sort package + sort.Ints(numbers) + fmt.Println(numbers) + }