Skip to content

Instantly share code, notes, and snippets.

@nwillc
Created July 14, 2022 14:31
Show Gist options
  • Save nwillc/c9ac92e31159dd059a3ef287cc7979fc to your computer and use it in GitHub Desktop.
Save nwillc/c9ac92e31159dd059a3ef287cc7979fc to your computer and use it in GitHub Desktop.
Example using generics based go query
{
// A simple student
type Student struct {
id int
grade int
name string
}
// Bind elements to result columns
binder := func(s *Student) []any {
return []any{&s.id, &s.name, &s.grade}
}
// Query
err, students := Query[Student](
db,
binder,
"SELECT id, name, grade FROM student WHERE grade != ?",
10)
// students now contains []*Student holding the query results
...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment