github.com/benma/gogen@v0.0.0-20160826115606-cf49914b915a/cmd/goexportdefault/README.md (about)

     1  # goexportdefault
     2  
     3  It is a common pattern in Go packages to implement a default instance of an exported struct and export functions that call the underlying default instance.
     4  
     5  A couple of examples from the stdlib:
     6  
     7   - `net/http` has `http.DefaultClient` and functions like `http.Get` just call the default `http.DefaultClient.Get`
     8   - `log` has `log.Logger` and functions like `log.Print` just call the default `log.std.Print`
     9  
    10  The exported package functions simply call the corresponding methods from the default instance.
    11  
    12  `goexportdefault` allows you to automatically generate a exported function for each method from a default struct.
    13  
    14  # Usage
    15  
    16  Given the following code:
    17  
    18  ```go
    19  var DefaultClient = New()
    20  //go:generate goexportdefault DefaultClient
    21  
    22  type Client struct {}
    23  
    24  func New() *Client {
    25    return &Client{}
    26  }
    27  
    28  // Do won't really do anything in this example
    29  func (c *Client) Do(interface{}) error {
    30    return nil
    31  }
    32  ```
    33  
    34  The it will automatically generate a `default_client_funcs.go` file with the following contents:
    35  
    36  ```go
    37  // Do is a wrapper around DefaultClient.Do
    38  func Do(v interface{}) error {
    39    return DefaultClient.Do(v)
    40  }
    41  ```