How to solve these coding exercises:


Coding Exercise #1

  1. Using the var keyword, declare 4 variables called a, b, c, d of type int, float64, bool and string.

  2. Using short declaration syntax declare and assign these values to variables x, y and z:

    - 20

    - 15.5

    - "Gopher!"

  3. Using fmt.Println() print out the values of a, b, c, d, x, y and z.

  4. Try to run the program without error.

Do you wonder what Gopher is?  Check it here: https://blog.golang.org/gopher

Are you stuck? Do you want to see the solution for this exercise? Click here.


Coding Exercise #2

Change the code from the previous exercise in the following way:

1. Declare a, b, c, d using a single var keyword (multiple variable declaration) for better readability.

2. Declare x, y and z on a single line -> multiple short declarations

3. Remove the statement that prints out the variables. See the error!

4. Change the program to run without error using the blank identifier (_)

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

func main() {
    var a float64 = 7.1

    x, y := true, 3.7

    a, x := 5.5, false

    _, _, _ = a, x, y
}

Are you stuck? Do you want to see the solution for this exercise? Click here.


Coding Exercise #4

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

version := "3.1"

func main() {
	name := 'Golang'
	fmt.Println(name)
}


Are you stuck? Do you want to see the solution for this exercise? Click here.