[generics] Trying to make some generic handler

105 views
Skip to first unread message

dol...@gmail.com

unread,
Aug 6, 2020, 10:23:57 PM8/6/20
to golang-nuts
Hi,

I'm quite happy about the generics so far, but now I'm trying some ideas that I don't know if they are possible or not given the current proposal.
My idea, is to create some kind of generic handler for "net/http" that could be used in a whole project.
It would allow same behavior on each routes (unmarshalling the request, validating it, calling the real business logic with a func(Request) (Response, error) style, handling the business logic error, marshalling the response).
This is currently impossible (that I know) without using some bad hacks (using an unmarshalling middleware and storing it in the context.Context), etc.

So here is my attempt using generics, but my example stopped on this error:

https://go2goplay.golang.org/p/NyvV47ozr7S

Ian Lance Taylor

unread,
Aug 7, 2020, 12:27:38 PM8/7/20
to dol...@gmail.com, golang-nuts
I don't understand how using generics with the current net/http
package will let you avoid the hacks that you mention.

The type errors your example encounters are due to the mismatch of
converting from an interface based approach to a type based approach.
I don't know that there is going to be any workable solution here.

Ian
Reply all
Reply to author
Forward
0 new messages