安裝 mySQL 介面
go get github.com/go-sql-driver/mysql
程式範例
package main
import (
_ "github.com/go-sql-driver/mysql"
"database/sql"
"fmt"
)
func main() {
db, err := sql.Open("mysql", "user:password@tcp(192.168.1.1:3306)/dbname?charset=utf8&allowOldPasswords=1")
checkErr(err)
rows, err := db.Query("SELECT `load` FROM a limit 10")
checkErr(err)
for rows.Next() {
var load int
err = rows.Scan(&load)
checkErr(err)
fmt.Println(load)
}
db.Close()
}
func checkErr(err error) {
if err != nil {
panic(err)
}
}
沒有留言:
張貼留言