Last active
July 17, 2019 00:21
-
-
Save ara-ta3/aeea309598e4844241125761d1169ac1 to your computer and use it in GitHub Desktop.
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
package main | |
import ( | |
"fmt" | |
_ "github.com/go-sql-driver/mysql" | |
"github.com/jmoiron/sqlx" | |
) | |
type Hoge struct { | |
ID int `db:"id"` | |
Name string `db:"name"` | |
} | |
func main() { | |
db, err := connect() | |
if err != nil { | |
panic(err) | |
} | |
s := "SELECT * FROM hoge WHERE id IN (?)" | |
q, vs, err := sqlx.In(s, []int{2, 3}) | |
// fmt.Printf("%+v\n", q) | |
// => SELECT * FROM hoge WHERE id IN (?, ?) | |
if err != nil { | |
panic(err) | |
} | |
var hs []Hoge | |
err = db.Select(&hs, q, vs...) | |
if err != nil { | |
panic(err) | |
} | |
fmt.Printf("%+v\n", hs) | |
// => [{ID:2 Name:fuga} {ID:3 Name:piyo}] | |
} | |
func connect() (*sqlx.DB, error) { | |
return sqlx.Connect( | |
"mysql", | |
fmt.Sprintf( | |
"%s:%s@tcp(%s:%s)/%s", | |
"root", | |
"", | |
"localhost", | |
"3306", | |
"test", | |
), | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
thanks for your example!