Help for the VSCode editor.
-
What is the most appropriate definition for recursion?
-
var my_func := (func(s string) {fmt.Println("Hey there,", s)})
-
var (my_func = func(s string) {fmt.Println("Hey there,", s)})
-
func main() { var ( my_func = func(s string) { fmt.Println("Hey there,", s) } ) my_func("Joe") }
-
var my_func func := (func(s string) {fmt.Println("Hey there,", s)})
Reveal
B, C
Quite simply, you can never use
:=
with avar
statement, leaving only two possible answers. -
-
What would be the output of the following program:
package main import ( "fmt" "strings" ) func main() { x := func(s string) string { return strings.ToUpper(s) } fmt.Printf("%T \n", x) fmt.Println(x("Joe")) }
- func(string) string
joe - func(string) string
JOE - string
Joe - func(string)
JOE
Reveal
func(string) string
JOE- Declare a variable
x
as an anonymous function that takes astring
, returns astring
with a body that returns the uppercase of what ever is passed as the argument. - Print the type of variable
x
, which will yield the function signature. A signature comprises:func
(
- List of argument types (if any)
)
- Return type(s) (if any) - the return types being a bracketed list if there is more then one.
- Print the invocation of the function via
x
, passing "Joe" as an argument. This is returned as "JOE" which gets printed.
- func(string) string
-
What would be the output of the following program:
package main import ( "fmt" "strings" ) func main() { x := func(s string) { fmt.Println(strings.ToLower(s)) } fmt.Printf("%T \n", x) x("RacheL") }
- func(string)
rachel - func(string) string
RACHEL - func(string) string
rachel - func(string)
Rachel
Reveal
func(string)
rachelVery similar to the previous question, however
- Variable
x
is assigned the anonymous function - The function handles the printing itself, therefore does not return anything, thus the signature that is printed is only
func(string)
- The function is lowercasing the value it is given.
- func(string)
-
What would be the output of the following program:
package main import ( "fmt" "strings" ) func main() { x := func(s string) { fmt.Println(strings.ToLower(s)) }("RacheL") fmt.Printf("%T \n", x) }
- func(string)
Rachel - Error
- func(string) string
rachel - func(string)
rachel
Reveal
Error
- The compiler detects an attempt to call the function during assignment of an anonymous function to a variable :-
}("RacheL)
. This is illegal so the program fails to compile.
- func(string)
-
What would be the output of the following program:
Add package and import statements as needed
var ( cube = func(i int) string { c := i * i * i return strconv.Itoa(c) } ) func main() { x := cube(8) fmt.Printf("%T %v", x, x) }
- func(i int) string
512 - int 512
- func(i int) int string
512 - string 512
Reveal
string 512
Note the use of
strconv.Itoa
. To build this program requires the additional import of"strconv"
. Recall from earlier labs thatItoa
converts an integer to its string representation.cube
is declared as a package variable that holds an anonymous function that takesint
and returnsstring
- The function computes the cube of
i
, converts that to a string and returns it. - In
main()
variablex
is assigned the value returned by callingcube
with 8. - Finally print the type and value of
x
.x
is the value returned bycube
, notcube
itself, therefore its type isstring
. The value ofx
is the string "512" so that's just printed. It's all on the same line since it's a singlePrintf
with no newline character in it.
- func(i int) string