Section11-5

This commit is contained in:
2023-11-17 13:28:33 +01:00
parent 12c600ef09
commit a2dc92f941
3 changed files with 97 additions and 0 deletions

24
Section11/89-5.go Normal file
View File

@@ -0,0 +1,24 @@
/*
Consider the following slice declaration:
nums := []int{5, -1, 9, 10, 1100, 6, -1, 6}
Using a slice expression and a for loop iterate over
the slice ignoring the first and the last two elements.
Print those elements and their sum.
*/
package main
import "fmt"
func main() {
nums := []int{5, -1, 9, 10, 1100, 6, -1, 6}
sum := 0
for _, v := range nums[2 : len(nums)-2] {
fmt.Println(v)
sum += v
}
fmt.Println("Sum:", sum)
}