github.com/aarzilli/tools@v0.0.0-20151123112009-0d27094f75e0/interface_example/doc.go (about)

     1  /*
     2  // http://jordanorelli.com/post/32665860244/how-to-use-interfaces-in-go
     3  
     4  // do something like this:
     5  type Entity interface {
     6      UnmarshalHTTP(*http.Request) error
     7  }
     8  
     9  func GetEntity(r *http.Request, v Entity) error {
    10      return v.UnmarshalHTTP(r)
    11  }
    12  
    13  // Where the GetEntity function takes an interface value
    14  // that is guaranteed to have an UnmarshalHTTP method.
    15  // To make use of this, we would define on our User object
    16  // some method that allows the User to describe
    17  // how it would get itself out of an HTTP request
    18  func (u *User) UnmarshalHTTP(r *http.Request) error {
    19     // ...
    20  }
    21  
    22  // in your application code,
    23  // you would declare a var of User type,
    24  // and then pass a pointer to this function into GetEntity
    25  var u User
    26  if err := GetEntity(req, &u); err != nil {
    27      // ...
    28  }
    29  */
    30  package extract