How to solve these coding exercises:


Coding Exercise #1

Consider the following variable declarations: 

x, y, z := 10, 15.5, "Gophers"
score := []int{10, 20, 30}

Using fmt.Printf():

  1. Print each variable using a specific verb for its type

  2. Print the string value enclosed by double quotes ("Gophers")

  3. Print each variable using the same verb

  4. Print the type of y and score

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


Coding Exercise #2

Consider the following constant declaration: const x float64 = 1.422349587101

Write a Go program that prints the value of x with 4 decimal points.

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() {
	shape := "circle"
	radius := 3.2
	const pi float64 = 3.14159
	circumference := 2 * pi * radius

	fmt.Printf("Shape: %q\n")
	fmt.Printf("Circle's circumference with radius %d is %b\n", radius, circumference)
	_ = shape
}

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