How to pass a function to a golang function? there is a function1 that you need to pass to function2 the parameters and function1 (or the name, so that you can then start the corresponding function), which you should perform after setting the parameters. how to do this?
- And at what stage did the difficulties arise? Signature? - D-side
- how to write that the function will accept the function as an input parameter? with types is clear, but here I don’t know how - Rakzin Roman
- I mean ... It's not clear how to write a type function ? - D-side
- Yes. In the rest of the string and int, and here what to write? Interface or how? - Rakzin Roman
|
1 answer
Everything is as usual, just without names and implementation:
func foo(x uint8, f func(uint8) uint16) uint16 { return f(x) }
If the signature turns out to be very large, and the type in the code is used often, you can not double the definition, but make an alias for this type (the general form: type имя тип
):
type transformer func(uint8) uint16 func foo(x uint8, f transformer) uint16 { return f(x) }
|