# I Built Gooogen to generate dynamic query methods

**URL:** <https://forum.golangbridge.org/t/i-built-gooogen-to-generate-dynamic-query-methods/41630>\
**Category:** Technical Discussion\
**Created:** [February 23, 2026, 1:22pm UTC](https://forum.golangbridge.org/t/i-built-gooogen-to-generate-dynamic-query-methods/41630 "2026-02-23T13:22:21Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![f0rb](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/f0rb/32/15478_2.png) [@f0rb](https://forum.golangbridge.org/u/f0rb)\
**Post date:** [February 23, 2026, 1:22pm UTC](https://forum.golangbridge.org/t/i-built-gooogen-to-generate-dynamic-query-methods/41630/1 "2026-02-23T13:22:21Z")

</div>

Usually, we need to write a lot of boilerplate code that builds dynamic queries through `if` statements. Conventional code generation methods can only generate equality conditions. I thought of solving this problem by adding a predicate suffix to fields, and created the tool `gooogen`. The usage is as follows:

```go
//go:generate gooogen
type UserQuery struct {
	PageQuery
	Name *string
	NameIn *[]string
	ScoreGe *int
	ScoreLt *int
}

```

Run `go generate` will generate the following code:

```go
func (q UserQuery) BuildConditions() ([]string, []any) {
	conditions := make([]string, 0, 4)
	args := make([]any, 0, 4)
	if q.Name != nil {
		conditions = append(conditions, "name = ?")
		args = append(args, *q.Name)
	}
	if q.NameIn != nil {
		phs := make([]string, 0, len(*q.NameIn))
		for _, arg := range *q.NameIn {
			args = append(args, arg)
			phs = append(phs, "?")
		}
		conditions = append(conditions, "name IN ("+strings.Join(phs, ", ")+")")
	}
	if q.ScoreGe != nil {
		conditions = append(conditions, "score >= ?")
		args = append(args, *q.ScoreGe)
	}
	if q.ScoreLt != nil {
		conditions = append(conditions, "score < ?")
		args = append(args, *q.ScoreLt)
	}
	return conditions, args
}

```

Combining it with entity objects in the ORM can build complete query statements, thereby simplifying the development of dynamic queries.

Feedback is welcome.

GitHub: [GitHub - doytowin/goooqo: GoooQo is a database access framework implemented in Go, based on OQM technique.](https://github.com/doytowin/goooqo)

---

<div class="post-metadata">

**Author:** ![system](https://forum.golangbridge.org/uploads/default/original/2X/5/5ad234f7c3dbc68b6cc72d338ce4f2973d9fd81c.png) [@system](https://forum.golangbridge.org/u/system)\
**Post date:** [May 24, 2026, 1:22pm UTC](https://forum.golangbridge.org/t/i-built-gooogen-to-generate-dynamic-query-methods/41630/2 "2026-05-24T13:22:31Z")

</div>

Closed on May 24, 2026, 1:22pm UTC

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
