Section11 Complete
This commit is contained in:
23
Section11/89-6.go
Normal file
23
Section11/89-6.go
Normal file
@@ -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)
|
||||||
|
|
||||||
|
}
|
||||||
24
Section11/89-7.go
Normal file
24
Section11/89-7.go
Normal file
@@ -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)
|
||||||
|
}
|
||||||
23
Section11/89-8.go
Normal file
23
Section11/89-8.go
Normal file
@@ -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)
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user