title: Optional parameters with defaults in Go struct constructors
body: <p>I've found myself using the following pattern as a way to get optional parameters with defaults in Go struct constructors:</p>
  
  <pre><code>package main
  
  import (
      "fmt"
  )
  
  type Object struct {
      Type int
      Name string
  }
  
  func NewObject(obj *Object) *Object {
      if obj == nil {
          obj = &amp;Object{}
      }
      // Type has a default of 1
      if obj.Type == 0 {
          obj.Type = 1
      }
      return obj
  }
  
  func main() {
      // create object with Name="foo" and Type=1
      obj1 := NewObject(&amp;Object{Name: "foo"})
      fmt.Println(obj1)
  
      // create object with Name="" and Type=1
      obj2 := NewObject(nil)
      fmt.Println(obj2)
  
      // create object with Name="bar" and Type=2
      obj3 := NewObject(&amp;Object{Type: 2, Name: "foo"})
      fmt.Println(obj3)
  }
  </code></pre>
  
  <p>Is there a better way of allowing for optional parameters with defaults?</p>