Application
Create new folder named src. In src folder, create new file named main.go as below:
package main
import "fmt"
func main() {
var a = [5]int{11, -4, 7, 8, -10}
result1, result2, result3, result4, result5 := count(a)
fmt.Println("Count elements: ", result1)
fmt.Println("Count positive elements: ", result2)
fmt.Println("Count negative elements : ", result3)
fmt.Println("Count even elements: ", result4)
fmt.Println("Count odd elements: ", result5)
}
func count(a [5]int) (int, int, int, int, int) {
counter1 := 0
counter2 := 0
counter3 := 0
counter4 := 0
counter5 := 0
for _, value := range a {
counter1++
if value > 0 {
counter2++
}
if value < 0 {
counter3++
}
if value%2 == 0 {
counter4++
}
if value%2 != 0 {
counter5++
}
}
return counter1, counter2, counter3, counter4, counter5
}
Output
Open Terminal windows in Visual Studio Code and run command line: go run main.go
Count elements: 5
Count positive elements: 3
Count negative elements : 2
Count even elements: 3
Count odd elements: 2