Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

redesign the execution function signature #6

Closed
plastikfan opened this issue Aug 8, 2023 · 0 comments · Fixed by #13
Closed

redesign the execution function signature #6

plastikfan opened this issue Aug 8, 2023 · 0 comments · Fixed by #13
Assignees
Labels
refactor Refactor code

Comments

@plastikfan
Copy link
Contributor

plastikfan commented Aug 8, 2023

The current function/interface needs a rework. Found a Golang Uk YouTube video (Golang UK Conference 2016 - Mat Ryer - Idiomatic Go Tricks, go to 8:20 function type alternatives for single method interfaces) that discusses this which I think can be well utilised here:

http.Handler has a counterpart called http.HandlerFunc

// in net.http
type Handler interface {
	ServeHTTP(ResponseWriter, *Request)
}

// The HanderFUnc 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)

// ServerHTTP calls f(w,r)
func (f HandlerFunc) ServeHTTP(w ResponseWriter, r *Request) {
	f(w, r)
}

this is intriguing and a technique I've not discovered before (defining a method on a function)

  • func type with matching signature
  • Method on that func implementing the interface
  • Method just calls the func
  • Now you don't even need a struct, you can just use a function
  • this pattern can be used when you have single method interfaces
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
refactor Refactor code
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant