How to solve these coding exercises:

Coding Exercise #1

  1. Declare a new type type called duration. Have the underlying type be an int.

  2. Declare a variable of the new type called hour using the var keyword

  3. In function main:

    1. print out the value of the variable hour

    2. print out the type of the variable hour

    3. assign 3600 to the variable hour using the  = operator

    4. print out the value of hour

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


Coding Exercise #2

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"

type duration int

func main() {
    var hour duration = 3600
    minute := 60
    fmt.Println(hour != minute)
}

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


Coding Exercise #3

  1. Declare two defined types called mile and kilometer. Have the underlying type be an float64.

  2. Declare a constant called m2km equals 1.609  ( 1mile=1.609km)

  3. In function main:

    1. declare a variable called mileBerlinToParis of type mile with value 655.3

    2. declare a variable called kmBerlinToParis of type kilometer

    3. calculate the distance between Berlin and Paris in km by multiplying mileBerlinToParis and m2km. Assign the result to kmBerlinToParis

    4. print out the distance in km between Berlin and Paris

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