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

     1  package extract
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  )
     7  
     8  type Extractable interface {
     9  	ExtractFromHtmlPost(r *http.Request) error
    10  }
    11  
    12  func Extract(r *http.Request, e Extractable) error {
    13  	return e.ExtractFromHtmlPost(r)
    14  }
    15  
    16  // ===============================================
    17  
    18  type User struct {
    19  	Name, Surname string
    20  }
    21  
    22  func (u *User) ExtractFromHtmlPost(r *http.Request) error {
    23  
    24  	if r == nil {
    25  		return fmt.Errorf("request is empty")
    26  	}
    27  
    28  	u.Name = "extracted from"
    29  	u.Surname = "http request"
    30  	return nil
    31  
    32  }
    33  
    34  func (u User) String() string {
    35  	return fmt.Sprintf("SName: %q LName: %q", u.Surname, u.Name)
    36  }