[Go]Indicate from 0 to 10 which is even and which is odd

This is an assignment 1 by Stephen Grider in his Go: The Complete Developer’s Guide (Golang) from udemy. By far the easiest to follow and least boring lesson about Go, well I said by far the easiest is because I bought a lot of courses from other instructors and none of them matches how Stephen Grider delivers the course.

So the code as follows:

package main

// Package to do printf
import "fmt"

// main is where is actual program will be compiled
func main() {
	// declared an integer slice, to me this relates to list in python.
	var num []int
	// initialized the i from 0.
	i := 0
	// count until the max of slice
	// there is no while loop in Golang, for is the assumed while loop
	for i != 11 {
		// append the new value i and update num.
		num = append(num, i)
		// count until 11
		i++
	}
	// not interested in index of a slice, but i am interested in j which is the value of the slice.
	for _, j := range num {
		// even number returns 0
		if j % 2 == 0 {
			fmt.Printf("%v is even\n", j)
		} else {
			fmt.Printf("%v is odd\n", j)
		}
	}

}
Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s