From 05649fb4d24922a4e00d7ec2c4cce318f1cc90e4 Mon Sep 17 00:00:00 2001 From: "Stephen M. McQuay" Date: Thu, 2 Aug 2012 14:39:13 -0600 Subject: [PATCH] added some sorting to the slices example --- slices/go.go | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/slices/go.go b/slices/go.go index 0edd533..78e0bd3 100644 --- a/slices/go.go +++ b/slices/go.go @@ -1,21 +1,25 @@ package main import "fmt" +import "sort" func main() { - p := []int{2, 3, 5, 7, 11, 13} - fmt.Println("p ==", p) + p := []int{2, 3, 13, 7, 5, 11} + fmt.Println("p ==", p) + sort.Ints(p) + fmt.Printf("%T p: %v\n", p, p) - for i := 0; i < len(p); i++ { - fmt.Printf("p[%d] == %d\n", i, p[i]) - } + for i := 0; i < len(p); i++ { + fmt.Printf("p[%d] == %d\n", i, p[i]) + } - // slice index notation: (python) - fmt.Println("p[1:4] == ", p[1:4]) - fmt.Println("p[:3] == ", p[:3]) - fmt.Println("p[4:] == ", p[4:]) + // slice index notation: (python) + fmt.Println("p[1:4] == ", p[1:4]) + fmt.Println("p[:3] == ", p[:3]) + fmt.Println("p[4:] == ", p[4:]) + + v := make([]string, 0) + v = append(v, "hello", "world") + fmt.Printf("%v\n", v) - v := make([]string, 0) - v = append(v, "hello", "world") - fmt.Printf("%v\n", v) }