Flashcards
Functions
Cards in this deck (30)
- 1Function declaration in GoUse the `func` keyword followed by the function name, input parameters with types, and return type.
- 2When is the `return` keyword required in a Go function?When the function returns a value or needs to exit early.
- 3How do you declare a function with no input parameters in Go?Use empty parentheses `()`.
- 4What should you do if a Go function has no return value?Leave the return type empty.
- 5Easier way to declare multiple parameters of the same type in Go?Specify the type once for all parameters.
- 6Can Go functions have named or optional parameters?No, Go does not support named or optional parameters.
- 7How can you simulate named parameters in Go?By defining a struct with fields for the desired parameters.
- 8What might be indicated by a function with too many parameters?It often indicates an overly complex function.
- 9What are variadic parameters in Go?Parameters indicated by `...` before the type, allowing multiple arguments.
- 10Where must variadic parameters appear in a Go function's parameter list?They must be the last parameter.
- 11How do you treat a variadic parameter as a slice in Go?You can use it like any other slice within the function.
- 12How do you supply a slice to a function expecting variadic parameters?Append `...` to the slice variable or literal.
- 13What happens if you omit `...` when passing a slice to a variadic function?It causes a compile-time error.
- 14How are multiple return values declared in a Go function?List the return types in parentheses, separated by commas.
- 15What is the convention for the last return value in a Go function with multiple returns?It is typically an error value, which should be `nil` if no error occurred.
- 16How do you handle unused return values in Go?Assign them to `_` to ignore them.
- 17Can you leave return values unassigned in Go?Yes, but only by omitting the assignment, which may reduce clarity.
- 18What are named return values in Go?Variables predeclared within the function to hold return values.
- 19What happens to named return values in Go upon function initialization?They are initialized to their zero values.
- 20Can you partially name return values in Go?Yes, you can use `_` for unnamed return values.
- 21What should you be cautious of when using named return values in Go?Accidentally shadowing the named return values within the function.
- 22Why should you avoid using blank returns in Go?They obscure data flow and make the code harder to understand.
- 23What does it mean that functions are values in Go?Functions can be assigned to variables and treated like any other value.
- 24What is the default value of a function variable in Go?`nil`, and running a `nil` function causes a panic.
- 25How do you define a function type in Go?Use the `type` keyword followed by the function signature.
- 26What are anonymous functions in Go?Functions without names, defined using the `func` keyword, that can be assigned to variables.
- 27When are anonymous functions useful in Go?For `defer` statements and launching goroutines.
- 28What are closures in Go?Functions declared inside other functions that can access and modify outer variables.
- 29What is the purpose of the `defer` keyword in Go?To schedule cleanup code that runs after a function completes.
- 30What does "Go is call by value" mean?Go copies variable values when passing them to functions, preventing the original variables from being modified.
Information
- Description
- Book Learning Go (CH5: Functions) Flashcards on Functions, created by Filipi Melo on 08/08/2024.
- Created by
- Filipi Melo
- Views
- 1
- Rating