Help for the VSCode editor.
See also Assignment in the Go manual.
All the code fragments in this lab are complete mini-programs, so you can paste them into the editor and run them to see the results:
All the code fragments in this lab are complete mini-programs, so you can paste them into the editor and run them to see the results:
Running the code fragments
- Right click in Explorer pane to create a new file, e.g.
test.go
- Paste the question code snippet into the editor pane
- Open the terminal window and execute
go run test.go
- Re-use your
test.go
file by replacing the content with that of the next question.
-
What would be the output for the following program:
package main import "fmt" func main() { var a, b string = "kolkata", "Kolkata" if a == b { fmt.Println("strings are equal") } else { fmt.Println("strings are not equal") } fmt.Println("thank you!") }
- strings are equal
- strings are equal
thank you! - Error
- strings are not equal
thank you!
Reveal
strings are not equal
thank you!Strings are not equal because there is a difference in capitalisation, so the
if
test isfalse
, and theelse
block is executed.There's no error as the syntax is correct and it will compile.
thank you!
is always printed, as it's not inside of an if-else construct. -
What would be the output for the following program:
package main import "fmt" func main() { var a, b string = "kolkata", "kolkata" if a == b { fmt.Println("strings are equal") } else { fmt.Println("strings are not equal") } fmt.Println("thank you!") }
- strings are equal
- strings are equal
thank you! - Error
- strings are not equal
thank you!
Reveal
Error
There is a syntax error in this code, so it will not compile. Go is strict about the placement of braces.
} else {
should be
} else {
-
What would be the output for the following program:
package main import "fmt" func main() { var a, b string = "foo", "bar" if a+b == "foo" { fmt.Println("foo") } else if a+b == "bar" { fmt.Println("bar") } else if a+b == "foobar" { fmt.Println("foobar") } else { fmt.Println("None matched") } fmt.Println("thank you!") }
- foo
thank you! - bar
thank you! - None matched
thank you! - foobar
thank you!
Reveal
foobar
thank you!- First, evaluate what is
a+b
. Addtion of strings concatenates them, so we getfoobar
- Now look at the if-else if statements. Which one matches
foobar
? This is the branch the program will follow.
thank you!
is always printed, as it's not inside of an if-else construct. - foo
-
What would be the output for the following program:
package main import "fmt" func main() { var a, b string = "foo", "bar" if a+b == "foo" { fmt.Println("foo") } else if a+b == "foobar" { fmt.Println("bar") } else if a+b == "foobar" { fmt.Println("foobar") } else { fmt.Println("None matched") } fmt.Println("thank you!") }
- None matched
thank you! - bar
thank you! - foobar
thank you! - foo
thank you!
Reveal
bar
thank you!This one has a gotcha! Note this bit
} else if a+b == "foobar" { fmt.Println("bar") } else if a+b == "foobar" { fmt.Println("foobar") } ...
a+b
is indeeedfoobar
and it matches both of the above. Go will choose the first match and ignore any others that match, therefore the output isbar
and notfoobar
- None matched
-
Can we use if block independently, without else-if and else blocks?
- No, we need at least one else block.
- else-if is optional, but if block would raise error without else block.
- Yes, we can write if block without any of the other blocks.
- We need at least one if, one else-if and one else block for program to compile successfully.
- false
true
Reveal
C
Just like speech. You don't always need to say "else" or "else if" after if.