這是我使用 go-sql-driver 的第一個腳本。我的 mysql 表(產品)看起來像:id intname varchar(255)IsMatch tinyint(1)created datetime我想簡單地從表中加載一行,并將其綁定到一個結構。到目前為止我有這個:package mainimport ( "database/sql" "fmt" _ "github.com/go-sql-driver/mysql")type Product struct { Id int64 Name string IsMatch ?????????? Created ?????}func main() { fmt.Printf("hello, world!\n") db, err := sql.Open("mysql", "root:@/product_development") defer db.Close() err = db.Ping() if err != nil { panic(err.Error()) // proper error handling instead of panic in your app } rows, err := db.Query("SELECT * FROM products where id=1") if err != nil { panic(err.Error()) // proper error handling instead of panic in your app }}現(xiàn)在我需要:1. What datatype in Go do I use for tinyint and datetime?2. How to I map the rows to a Product struct?
將struct映射到mysql表,并將行綁定到struct
慕無忌1623718
2021-07-28 17:42:58