The following exercises let you try out code with blocks and if statements.
-
Create a game where the computer picks a number and then tries to guess it. Define a variable named luckyNumber and set it to rand.IntN(10)+1. Define a second variable myGuess and also set it to rand.IntN(10)+1. Write a series of if statements that print out if myGuess is equal to, less than, or greater than luckyNumber.
-
Rewrite the following code so err is scoped to only the if/else chain and is not accessible after it:
go
err:= doSomething()
if err != nil { fmt.Println("failed:", err) } else { fmt.Println("ok") }
- Declare a local variable named len and write code that uses both the predeclared identifier and the shadow. Explain what happens if you call len("hi") after the shadow.
Progress is saved on this device.