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
Using the var keyword, declare 4 variables called a, b, c, d of type int, float64, bool and string.
Using short declaration syntax declare and assign these values to variables x, y and z:
- 20
- 15.5
- "Gopher!"
Using fmt.Println() print out the values of a, b, c, d, x, y and z.
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.