1.9 KiB
How to fix this program?
package main
import (
"fmt"
"time"
)
func main() {
if err != nil; d, err := time.ParseDuration("1h10s") {
fmt.Println(d)
}
}
- Swap the simple statement with the
err != nil
check. CORRECT - Remove the error handling.
- Remove the semicolon.
- Change the short declaration to an assignment.
1: Yes. In a short if statement, the simple statement (the short declaration there) should be the first part of it. Then, after the semicolon separator, there should be a condition expression.
2: You don't want that. That's not the issue here.
What does this program print?
package main
import "fmt"
func main() {
done := false
if done := true; done {
fmt.Println(done)
}
fmt.Println(done)
}
- true and true
- false and false
- true and false CORRECT
- false and true
3: Yes. It shadows the main()'s done variable, and inside the if statement, it prints "true". Then, after the if statement ends, it prints the main()'s done variable which is "false".
How can you fix this code?
package main
import "fmt"
func main() {
done := false
if done := true; done {
fmt.Println(done)
}
fmt.Println(done)
}
- Remove the first declaration (main()'s done)
- Remove the declaration in the short-if (if's done)
- Change the done declaration of the main() to an assignment
- Change the done declaration of the short-if to an assignment. And, after the if statement, assign false back to the done variable. CORRECT
1: That will break the program. The last line tries to print it.
2: The program wants to use it to print true.
3: There will be "undefined variable" error. 4: Yes, that will solve the shadowing issue. Short-if will reuse the same done variable of the main(). And, after the short-if, done will be false because of the assignment, and it will print false.