Created
February 6, 2020 02:44
-
-
Save estenssoros/786c4607ebdbc246648f1751e7fdd160 to your computer and use it in GitHub Desktop.
This file contains 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
package main | |
import ( | |
"fmt" | |
"log" | |
"reflect" | |
"strings" | |
) | |
type model struct { | |
ID int | |
} | |
func joinIds(slice interface{}, idFunc func(i int) string, joiner string) string { | |
rv := reflect.ValueOf(slice) | |
ids := make([]string, rv.Len()) | |
for i := 0; i < rv.Len(); i++ { | |
ids[i] = idFunc(i) | |
} | |
return strings.Join(ids, joiner) | |
} | |
func run() { | |
models := []model{} | |
query := "SELECT id FROM blah WHERE id in (%s)" | |
f := func(i int) string { return fmt.Sprint(models[i].ID) } | |
rows, err := db.Query(fmt.Sprint(query, joinIds(models, f, ","))) | |
... | |
... | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment