type MultiplyStruct struct {
Number1 int
}
func (m MultiplyStruct) Multiply() int {
return m.Number1 * number2
}
how can I pass number2
to the Multiply function? Would it be like this?
var multiplier = MultiplyStruct(10)
multiplier.Multiply(20)
I think this is what you want:
package main
import (
"fmt"
)
type MultiplyStruct struct {
Number1 int
}
func (m MultiplyStruct) Multiply(number2 int) int {
return m.Number1 * number2
}
func main() {
multipler := MultiplyStruct{Number1: 10}
val := multipler.Multiply(20)
fmt.Println(val)
}
Just an interesting addition to the correct answer. You can create a curried function without using a struct to store only an integer:
func Multiply(number1 int) (func(int) int) {
return func(number2 int) int {
return number1 * number2
}
}
func main() {
timesTen := Multiply(10)
fmt.Println(timesTen(20)) // => 200
timesTwo := Multiply(2)
fmt.Println(timesTwo(10)) // => 20
fmt.Println(Multiply(2)(3)) // => 6
}