Challenge: Reviewing Concepts of Functions
Swipe to show menu
Task
- Finish the code for the second parameter of the
operatefunction, which is expected to accept thecapitalizefunction; - Review the code and provide suitable arguments in the
operatefunction call within themain()function; - Save the returned values from the
operatefunction call into the variablesoriginalandupdated.
index.go
1234567891011121314151617181920212223package main import "fmt" // `ToLower()` and `ToUpper()` functions are imported from the `strings` module import "strings" func capitalize(str string) string { return strings.ToUpper(str) } func operate(str string, operation ___) (string, string) { return str, operation(str) } func main() { inputString := "Hello World" var original string var updated string ___ = operate(___) fmt.Println(original, updated) }
Hint
Pass inputString and capitalize to the operate function.
Solution
package main
import "fmt"
// ToLower() and ToUpper() functions are imported from the `strings` module
import "strings"
func capitalize(str string) string {
return strings.ToUpper(str)
}
func operate(str string, operation func(string) string) (string, string) {
return str, operation(str)
}
func main() {
inputString := "Hello World"
var original string
var updated string
original, updated = operate(inputString, capitalize)
fmt.Println(original, updated)
}
Everything was clear?
Thanks for your feedback!
Section 4. Chapter 7
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Section 4. Chapter 7