本文主要是介绍Go操作Mysql基础-,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
package main
import (
"database/sql"
"fmt"
_ "github.com/go-sql-driver/mysql"
"learngo/mysql/model"
)
func main() {
var product model.Product
db, err := sql.Open("mysql", "root:123456@tcp(localhost:3306)/laravel_admin?charset=utf8")
if err != nil {
fmt.Println(">>> fail to connect to db <<<")
}
defer db.Close()
fmt.Println(">>> succeed to connect to db <<<")
err = db.QueryRow("select `name`,`price`,`created_at`,`intro`,`id` from product where id =?", 4).Scan(&product.Name, &product.Price, &product.CreatedAt, &product.Intro, &product.Id)
if err != nil {
fmt.Println(err)
}
fmt.Println(product.Name.String)
}
这篇关于Go操作Mysql基础-的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!