Search code examples
gomiddleware

Middleware pattern in Go - 'used as value' compile error


I am trying to make middleware to my functions similar to the pattern used for http.HandlerFunc, this usually works:

func middleware(fn http.HandlerFunc) http.HandlerFunc {
    return func (wr http.ResponseWriter, r *http.Request) {
        fmt.Println("doing something before")
        fn(wr, r)
    }
}

func main() {
    middleware(func (wr http.ResponseWriter, r *http.Request) {
        wr.Write([]byte("..."))
    })
}

This doesn't work:

package main

import (
    "fmt"
)

type FN func (arg int)

func myFunc(arg int) {
    fmt.Println("Arg:",arg)
}
// Logically here I am returning the function not it's value
// http package does this identically, I don't see the difference
func do(fn FN) FN {
    return func (arg int) {
        fn(arg)
    }
}

func main() {
    do(myFunc(3))
}

Will return compile error: myFunc(3) used as value

As you can see here: https://golang.org/src/net/http/server.go?s=64103:64150#L2055

On line 2065:

// The HandlerFunc type is an adapter to allow the use of
// ordinary functions as HTTP handlers. If f is a function
// with the appropriate signature, HandlerFunc(f) is a
// Handler that calls f.
type HandlerFunc func(ResponseWriter, *Request)

This function signature also does not return a value yet this compiles.

UPDATE:

This pattern is what I was trying to achieve, which now works.

package main

import (
 "fmt"
)

type FN func (arg int)
func do(fn FN) FN {
    return func (arg int) {
    fmt.Println("executed do with arg",arg)
    // some other code ...
    fn(arg) // call wrapped function
   }
}
func something(arg int){
fmt.Println("Executed something with arg",arg)
}

func main() {
   do(something)(3)
}

Output:

executed do with arg 3
Executed something with arg 3

Program exited.

Solution

  • You are using myFunc(arg int) - which doesn't return anything, and trying to pass it's return value into do. It looks like maybe you want to do:

    func main() {
        do(myFunc)(3)
    }
    

    but not really sure