Created
July 14, 2022 14:31
-
-
Save nwillc/c9ac92e31159dd059a3ef287cc7979fc to your computer and use it in GitHub Desktop.
Example using generics based go query
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
// 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