undefinedfix
Sign in

What's wrong with passing in a function as a function parameter in golang?

detoro84 edited in Mon, 11 Jul 2022

图片描述

After reading it for a long time, I don't think it's a problem. It's right to pass in countfunc as a parameter with two int types. Why do you report this error when you pass it in~

2 Replies
HelloWorld
commented on Mon, 11 Jul 2022

Thank you for your invitation

1 function prototype includes return value type 2 golang does not support named arguments

package main

import (
    "fmt"
)

func countFunc(a int, b int) (c, d int) {
    return a + 1, b + 1
}

func mixFunc(op func(int, int) (int, int), a int, b int) (int, int) {
    return op(a, b)
}
func main() {

    q, r := mixFunc(countFunc, 2, 3)
    fmt.Printf("%d %d\n", q, r)
} 
jakab922
commented on Mon, 11 Jul 2022

The type of the incoming function does not match the type of the receiving function. The function prototype includes the type of the return value. Just change func mixfunc (OP func (int, int), a, B int) (Q, R int) {to func mixfunc (OP func (int, int) (C, D int), a, B int) (Q, R int) {