然后再
db.Close()
这样每次打开、关闭 再打开再关闭,好像性能不好。
有没有办法所有地方的mysql操作都用一个 db连接 ?
我毫尝试写一个全局的db,但是当程序运行一会儿就会报错:panic: packet sequence error
package common import ( "errors" //"fmt" "github.com/ziutek/mymysql/mysql" _ "github.com/ziutek/mymysql/native" // Native engine // "os/exec" "strconv" "time" ) var dbConnection mysql.Conn func GetMysqlConnection(dbname string) (mysql.Conn, error) { if dbConnection != nil { return dbConnection, nil } defaultConfig := MysqlConnectionConfigList[index] db := mysql.New("tcp", "", "127.0.0.1:3306", "root", "123456", dbname) db.Register("set names utf8") db.SetTimeout(10 * time.Second) err := db.Connect() if err != nil { return nil, err } dbConnection = db return db, nil }