title: Build dynamic (conditional) WHERE SQL query in Golang
body: <p>I'm using golang, go_reform, PostgreSQL. What i want to do is a REST search utility, and all went fine until I faced with conditional search query. "Conditional" here means I have 10 columns in a table to search in, and there may be ton of combinations so I can't handle them all separately. What i need is a query builder, but I have no understanding how can I implement this in Go. For now I have an idea like this, but it seems not really efficient</p>
  
  <pre><code>type Query struct {
      Id               *int64
      FirstName        *string
      MiddleName       *string
      LastName         *string
      AreaId           *int64
      Birthday         *time.Time
  }
  
  func (table *Query) Find() (*User) {
      if table.Id != nil {
          idstr := fmt.Sprintf("WHERE Id = %d AND ", table.Id)
      }
      else idstr := "WHERE "
      }
      if table.FirstName != "" {
          firststr := fmt.Sprintf("FirstName = %s AND", table.FirstName)
      }
      else firststr := ""
  }//and so on
  </code></pre>
  
  <p>That feels really awkward so I'm wondering is there any better way to determine the fields that came to the <code>Find()</code> and build a SQL query based on this. (Actually it's coming in JSON and binding to <code>Query</code> struct, so maybe there is a way without struct). There also may be SQL workarounds, but I think it would be more efficient to build query without all possible columns.</p>
  
  <p><strong>EDIT:</strong> By the way, making my Google search query more accurate, i found a bunch of things related to my problem, probably i will try to use it now. For those who interested too:
  <a href="https://play.golang.org/p/QN0S_O4dqe" rel="noreferrer">old go playground example</a></p>
  
  <p><a href="https://stackoverflow.com/questions/46859705/making-dynamic-queries-in-golang-and-mysql">Making dynamic SQL queries to a MySQL DB</a></p>
  
  <p><a href="https://github.com/go-gorp/gorp" rel="noreferrer">gorp package</a> (snippets thing sounds very promising)</p>