Help for the VSCode editor.
-
Select the correct statement(s) for defer statement.
defer
statement delays the execution of a function until the previous function returns.defer
statement delays the execution of a function until the following function returns.defer
statement delays the execution of a surrounding function until the current function returns.defer
statement delays the execution of a function until the surrounding function returns.
Reveal
D
func afunction() { // This function is surrounding the folliowing defer defer fmt.Println("defer executed") fmt.Println("this will print first") } // defer executes here
-
What would be the output of the following program?
package main import "fmt" func printString(str string) { fmt.Printf("%q ", str) } func printInt(i int) { fmt.Printf("%d ", i) } func printFloat(f float64) { fmt.Printf("%.2f ", f) } func main() { printString("browser") defer printInt(32) defer printFloat(0.24) printString("chrome") printInt(90) defer printFloat(89) printInt(900) }
- "browser" "chrome" 90 0.24 32 900 89.00
- browser chrome 90 900 89.00 0.24 32
- "browser" "chrome" 90 900 89.00 0.24 32
- browser chrome 0.24 32 90 900 89.00
Reveal
"browser" "chrome" 90 900 89.00 0.24 32
printString
uses the%q
formatter meaning that strings are printed in quotes. This rules out two possible answers.- Recall that
defer
statements execute after everything else in the function, so this means that "browser", "chrome" and 90 will be output first. - Recall that multiple
defer
statements execute in the reverse order to which they are declared. This a "stack" behaviour: last in first out. This tells us that the order of output from the defers will be 89.00, 0.24, 32 - yielding the correct answer.
-
What would be the output of the following program?
package main import ( "fmt" "strings" ) func getString(str string) (string, string) { return strings.ToLower(str), strings.ToUpper(str) } func main() { _, lower := getString("BROWSER") fmt.Println(lower) }
- browser
- error
- BROWSER
- Browser
Reveal
BROWSER
- The function
getString
returns two values:- Lower case version of the input
- Upper case version of the input
- When the function is called in
main
, the assignment is to_, lower
. - The first return value (the lowercase version) is discarded (assigned to
_
). - The second return value (the uppercase version) is assigned to
lower
, which is then printed.
-
Make the required changes in the function body:
func greetings() (x, y string) { x := "hello " y := "world" } func main() { fmt.Print(greetings()) }
...so the output is
hello world
. Select the corect definitions:-
func greetings() (x, y string) { x := "hello " y := "world" return x, y }
-
func greetings() (x, y string) { x = "hello " y = "world" return x, y }
-
func greetings() (x, y string) { x = "hello " y = "world" return }
-
func greetings() (x, y string) { x := "hello " y := "world" return "hello world" }
Reveal
B, C
- A is incorrect because
x
andy
are named return values, therefore they cannot be reassigned with:=
- B is correct because the values are correctly assigned. You can return the named variables by name.
- C is correct because the values are correctly assigned, and the named variables are implicitly returned.
- D is incorrect for the same reason as A, and also because it only returns one value when two are expected.
-
-
What would be the output of the following program?
package main import ( "fmt" ) func main() { fmt.Println(f1()) } func f1() int { return f2() } func f2() int { return 1 }
- 1 1
- no output
- error
- 1
Reveal
1
Starting from
main
- It prints the return value of
f1()
f1
returns the functionf2
f2
returns1
Thus the
fmt.Println
is actually callingf2
as it's what is returned byf1
.