forked from ardanlabs/gotraining
-
Notifications
You must be signed in to change notification settings - Fork 0
/
exercise1.go
44 lines (34 loc) · 1.14 KB
/
exercise1.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// All material is licensed under the Apache License Version 2.0, January 2004
// http://www.apache.org/licenses/LICENSE-2.0
// Declare a nil slice of integers. Create a loop that appends 10 values to the
// slice. Iterate over the slice and display each value.
//
// Declare a slice of five strings and initialize the slice with string literal
// values. Display all the elements. Take a slice of index one and two
// and display the index position and value of each element in the new slice.
package main
import "fmt"
func main() {
// Declare a nil slice of integers.
var numbers []int
// Append numbers to the slice.
for i := 0; i < 10; i++ {
numbers = append(numbers, i*10)
}
// Display each value.
for _, number := range numbers {
fmt.Println(number)
}
// Declare a slice of strings.
names := []string{"Bill", "Joan", "Jim", "Cathy", "Beth"}
// Display each index position and name.
for i, name := range names {
fmt.Printf("Index: %d Name: %s\n", i, name)
}
// Take a slice of index 1 and 2.
slice := names[1:3]
// Display the value of the new slice.
for i, name := range slice {
fmt.Printf("Index: %d Name: %s\n", i, name)
}
}