Skip to content

[survey] Why HandlerFunc use value receiver instead of pointer #30

Open

Description

@at15

Arise from #26

When looking at apex/log's code (which use a net/http like handler style instead of logrus like formatter writer style), found the handy util type HandlerFunc func... which also appears in net/http

https://golang.org/src/net/http/server.go#L1914

type Handler interface {
  	ServeHTTP(ResponseWriter, *Request)
 }

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

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

The question is, why use func (f HandlerFunc) instead of func (f *HandlerFunc)

Activity

  1. self-assigned this
    on Dec 19, 2017
  2. added a commit that references this issue on Dec 22, 2017
  3. added this to the future milestone on Feb 3, 2018
  4. modified the milestones: future, 0.0.8 on Nov 25, 2018
  5. removed this from the 0.0.8 milestone on Dec 9, 2018
  6. added this to the 0.0.10 milestone on Dec 31, 2018
  7. modified the milestones: 0.0.10, future on Apr 13, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions