Section11-2
This commit is contained in:
21
Section11/89-1.go
Normal file
21
Section11/89-1.go
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
/*
|
||||||
|
Coding Exercise #1
|
||||||
|
|
||||||
|
Using a composite literal declare and initialize
|
||||||
|
a slice of type string with 3 elements.
|
||||||
|
|
||||||
|
Iterate over the slice and print each element in
|
||||||
|
the slice and its index.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package main
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
countries := []string{"Romania", "Brazil", "Germany"}
|
||||||
|
for i, v := range countries {
|
||||||
|
fmt.Printf("Index: %d, Element: %q\n", i, v)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
45
Section11/89-2.go
Normal file
45
Section11/89-2.go
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
/*
|
||||||
|
Coding Exercise #2
|
||||||
|
|
||||||
|
There are some errors in the following Go program. Try to identify the errors,
|
||||||
|
change the code and run the program without errors.
|
||||||
|
|
||||||
|
package main
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
mySlice := []float64{1.2, 5.6}
|
||||||
|
|
||||||
|
mySlice[2] = 6
|
||||||
|
|
||||||
|
a := 10
|
||||||
|
mySlice[0] = a
|
||||||
|
|
||||||
|
mySlice[3] = 10.10
|
||||||
|
|
||||||
|
mySlice = append(mySlice, a)
|
||||||
|
|
||||||
|
fmt.Println(mySlice)
|
||||||
|
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
package main
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
mySlice := []float64{1.2, 5.6}
|
||||||
|
|
||||||
|
mySlice[1] = 6
|
||||||
|
|
||||||
|
a := 10
|
||||||
|
mySlice[0] = float64(a)
|
||||||
|
|
||||||
|
mySlice[1] = 10.10
|
||||||
|
|
||||||
|
mySlice = append(mySlice, float64(a))
|
||||||
|
|
||||||
|
fmt.Println(mySlice)
|
||||||
|
|
||||||
|
}
|
||||||
17
Section11/89-3.go
Normal file
17
Section11/89-3.go
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
/*
|
||||||
|
Coding Exercise #3
|
||||||
|
|
||||||
|
1. Declare a slice called nums with three float64 numbers.
|
||||||
|
|
||||||
|
2. Append the value 10.1 to the slice
|
||||||
|
|
||||||
|
3. In one statement append to the slice the values: 4.1, 5.5 and 6.6
|
||||||
|
|
||||||
|
4. Print out the slice
|
||||||
|
|
||||||
|
5. Declare a slice called n with two float64 values
|
||||||
|
|
||||||
|
6. Append n to nums
|
||||||
|
|
||||||
|
7. Print out the nums slice
|
||||||
|
*/
|
||||||
Reference in New Issue
Block a user