golang middleware example https://play.golang.org/p/1Rcyps1TRwO
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
) | |
type Service interface { | |
Do() | |
} | |
type service struct {} | |
func (s service) Do() { | |
fmt.Println("service.Do()") | |
} | |
type metricMiddleware struct { | |
name string | |
next Service | |
} | |
func (m metricMiddleware) Do() { | |
fmt.Printf("metricMiddleware.Do(), metric: %s\n", m.name) // the middleware injected | |
m.next.Do() | |
} | |
type traceMiddleware struct { | |
name string | |
next Service | |
} | |
func (t traceMiddleware) Do() { | |
fmt.Printf("traceMiddleware.Do(), trace: %s\n", t.name) // the middleware injected | |
t.next.Do() | |
} | |
func main() { | |
var s Service | |
s = service{} | |
s = metricMiddleware{"request_count", s} | |
s = traceMiddleware{"request", s} | |
s.Do() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment