From 1c925bc32a3a20effa62695bf9f5f1c1562a4f0f Mon Sep 17 00:00:00 2001 From: sebastian Date: Thu, 23 Nov 2023 13:01:04 +0100 Subject: [PATCH] Section11 Complete --- Section11/89-6.go | 23 +++++++++++++++++++++++ Section11/89-7.go | 24 ++++++++++++++++++++++++ Section11/89-8.go | 23 +++++++++++++++++++++++ 3 files changed, 70 insertions(+) create mode 100644 Section11/89-6.go create mode 100644 Section11/89-7.go create mode 100644 Section11/89-8.go diff --git a/Section11/89-6.go b/Section11/89-6.go new file mode 100644 index 0000000..c66f948 --- /dev/null +++ b/Section11/89-6.go @@ -0,0 +1,23 @@ +/* +Consider the following slice declaration: +friends := []string{"Marry", "John", "Paul", "Diana"} + +Using copy() function create a copy of the slice. Prove that the slices +are not connected by modifying one slice and notice that the other slice is not modified. +*/ + +package main + +import "fmt" + +func main() { + friends := []string{"Marry", "John", "Paul", "Diana"} + + yourFriends := make([]string, len(friends)) + copy(yourFriends, friends) + + yourFriends[0] = "Dan" + + fmt.Println(friends, yourFriends) + +} diff --git a/Section11/89-7.go b/Section11/89-7.go new file mode 100644 index 0000000..4990b3b --- /dev/null +++ b/Section11/89-7.go @@ -0,0 +1,24 @@ +/* +Consider the following slice declaration: +friends := []string{"Marry", "John", "Paul", "Diana"} + +Using append() function create a copy of the slice. +Prove that the slices are not connected by modifying +one slice and notice that the other slice is not modified. +*/ + +package main + +import "fmt" + +func main() { + friends := []string{"Marry", "John", "Paul", "Diana"} + + yourFriends := []string{} + + yourFriends = append(yourFriends, friends...) + + yourFriends[0] = "Dan" + + fmt.Println(friends, yourFriends) +} diff --git a/Section11/89-8.go b/Section11/89-8.go new file mode 100644 index 0000000..a334171 --- /dev/null +++ b/Section11/89-8.go @@ -0,0 +1,23 @@ +/* +Consider the following slice declaration: + + years := []int{2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010} + +Using a slice expression and append() function create a new slice called +newYears that contains the first 3 and the last 3 elements of the slice. +newYears should be []int{2000, 2001, 2002, 2008, 2009, 2010} +*/ + +package main + +import "fmt" + +func main() { + years := []int{2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010} + newYears := []int{} + + newYears = append(years[:3], years[len(years)-3:]...) + + fmt.Printf("%#v\n", newYears) + +}