Hi Cherolyn,
Out of curiosity, are you working through a course authored by Todd McLeod? I recognise this exercise.
In regards to the exercise, the requirement is that you have a function that takes another function as an argument.
So, first up let’s create a function.
func doSomething() {
}
So, we have a function called doSomething
which ironically currently does nothing. In order to be able to pass a function as an argument, we need to define that between the parentheses. We can do that like this:
func doSomething(something func()) {
}
So, we’re telling Go that the function doSomething
expects another function to be passed as an argument. For the sake of simplicity the function that we pass into doSomething
doesn’t return anything or take any arguments itself.
Now we know that whenever doSomething
is called we’ll have an argument something
which is also a function, we can again for the sake of simplicity just call it.
func doSomething(something func()) {
something() // we can call this because we know it's always a function
}
What this means is that, whenever we call doSomething
passing another function as an argument, the doSomething
function will immediately call the function you gave it.
In order to test this we can create another function that does nothing but print out a string.
function somethingToDo() {
fmt.Println("We're doing something cool!")
}
Because the function doesn’t take any arguments or return anything, we can use it as the argument for the doSomething
function. Like this:
doSomething(somethingToDo)
and if everything went to plan, you should see in the logs
We're doing something cool!
The above example can be found here: Go Playground - The Go Programming Language
Now, let’s try and apply the above to what you’re trying to achieve.
My idea was, and I have no idea where this idea came from, was to add 42 to each of these numbers: 1, 2, 3, 4, 5, 6, 7, 8, 9
First up, let’s create a function that takes a number as an argument and returns that number plus 42.
func add42(number int) int {
return number + 42;
}
The above function takes a given integer number
and returns number + 42
. We define number
of type int
as an argument to the function and tell Go that we expect add42
to return an int
by adding it as the return type after the parentheses.
For example, if we were to now call add42
on the number 10
and print out the value using fmt.Println
we would get 52.
fmt.Println(add42(10)) // 52
So, now we have a function that does something useful, we now need to create another function that will take an array of integers and a function and call the function on each of the integers in the array and return the subsequently returned values.
In the context of your idea, we’ll create a function that we pass []int{ 1, 2, 3, 4, 5, 6, 7, 8, 9 }
and the add42
function and expect to get an array back containing: int[]{ 43, 44, 45, 46, 47, 48, 49, 50, 51 }
.
// numbers = an array of input numbers 1, 2, 3, 4...
//
// add is a function argument for a function that takes an integer as
// an argument and returns an integer. We'll be passing the add42 function here.
//
// []int is the return type which is `numbers` with 42 added to each.
func add42ToAll(numbers []int, add func(int) int) []int {
// create an array to hold our new numbers
newNumbers := []int{}
// loop over the numbers input array
for _, number := range numbers {
// for each `number` in the numbers array, add the value returned from
// add (add42) to the `newNumbers` array.
newNumbers = append(newNumbers, add(number))
}
// return our newNumbers
return newNumbers
}
If we then call the add42ToAll
function with []int{ 1, 2, 3, 4, 5, 6, 7, 8, 9 }
and passing add42
as the add
function argument, we should get what we want.
Here’s an example of the above in practice: Go Playground - The Go Programming Language
I hope this was helpful and it’s great to see that you’re learning Go!