How to solve these coding exercises:
Write your solution in Go Playground (https://play.golang.org) and then click on the Run button.
If your solution is not correct, then try to understand the error messages, rewrite the solution, and press the Run button again. Repeat this step until you get the correct solution.
Each exercise should be solved in Go Playground in its own file. Save the URL of each file for future reference or recap. Alternatively, you can write the solution in VSCode and run the program in terminal using: go run main.go
Coding Exercise #1
1. Using the var keyword, declare an array called cities with 2 elements of type string. Don't initialize the array.
Print out the cities array and notice the value of its elements.
2. Declare an array called grades of type [3]float64 and initialize only the first 2 elements using an array literal.
Print out the grades array and notice the value of its elements.
3. Declare an array called taskDone using the ellipsis operator. The elements are of type bool. Print out taskDone.
4. Iterate over the array called cities using the classical for loop syntax and the len function and print out element by element.
5. Iterate over grades using the range keyword and print out element by element.
Are you stuck? Do you want to see the solution for this exercise? Click here.
Coding Exercise #2
Consider the following array declaration: nums := [...]int{30, -1, -6, 90, -6}
Write a Go program that counts the number of positive even numbers in the array.
Are you stuck? Do you want to see the solution for this exercise? Click here.
Coding Exercise #3
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() { myArray := [3]float64{1.2, 5.6} myArray[2] = 6 a := 10 myArray[0] = a myArray[3] = 10.10 fmt.Println(myArray) }
Are you stuck? Do you want to see the solution for this exercise? Click here.