-
Notifications
You must be signed in to change notification settings - Fork 18k
net/http: avoid casting and extra function calling (http.HandleFunc, http.DefaultServeMux.HandleFunc) #25706
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
Comments
@ntrrg The golang/go issues are meant for bugs or feature requests - not general questions. I recommend closing this issue and taking the following steps until you get a satisfactory answer to your question:
|
Great, thanks! 😄 I was a little confused because this is actually more a proposal than a question. |
This is definitely the right place to create a proposal, but to be honest your proposal is not very clear to me. I leaned toward assuming this was just a question because of the word "wonder". If you'd like to try again to explain, please reply with more detail on what exactly you're proposing. |
Your are right 😆 I will try to make it clearer, so please, read the description again. I am still learning English btw 😅 |
Please send a change when the Go 1.12 cycle opens in a few months. Thank you. |
Great! 😄 I suppose that this issue should keep open, right? |
Yes, please keep open. |
As it stands now, the proposed change cannot happen in Go 1, as it is changing the signature of public methods.
type HandlerFunc = func(ResponseWriter, *Request)
Type alias makes no sense, as |
@bontibon Actually it already exists here, that is the reason of this proposal 😄 |
@FiloSottile I think that this could be added in the next release because doesn't break any code and is invisible to any user. But this needs a modification in api/go1.txt, this file can't be modified? Even if the modification doesn't break the API? |
Go 1.11 is frozen. https://github.com/golang/go/wiki/Go-Release-Cycle. |
Yep, sorry, the next next release (1.12) i mean 😆 |
This is an API change that is forbidden by the Go 1 guarantee. It would break code that said var x func(string, func(http.ResponseWriter, *http.Request)) = http.HandleFunc So this issue is correctly labeled as a Go 2 issue. Closing this issue in favor of #5465, where this change could be made as part of a future net/http/v2. |
Yes, you are right, sorry about that 😅 |
Looking at the
net/http
package I notice thatHandleFunc
andServeMux.HandleFunc
have afunc(ResponseWriter, *Request)
as parameter instead ofHandlerFunc
, this means that this functions need to call one extra fuction and an explicit casting respectively.So if they are written in this way, it can be avoided:
HandleFunc
Since
HandlerFunc
implements theHandler
interface,DefaultServeMux.HandleFunc
should be replaced byDefaultServeMux.Handle
.From:
To:
ServeMux.HandleFunc
Since the
handler
argument is implicitly casted (or something else happens here?), an explicit casting is not necessary.From:
To:
The text was updated successfully, but these errors were encountered: