package dao import ( "Zythum/config" _ "github.com/go-sql-driver/mysql" "github.com/jmoiron/sqlx" "gopkg.in/yaml.v3" "os" ) var ( db *sqlx.DB ) func init() { data, err := os.ReadFile("config/datasource.yaml") if err != nil { panic("Failed to read datasource.yaml, please check the file.") } var dbConfig config.DBConfig err = yaml.Unmarshal(data, &dbConfig) if err != nil { panic("Failed to unmarshal datasource.yaml, please check the file.") } dsn := dbConfig.MySQLConfig.User + ":" + dbConfig.MySQLConfig.Password + "@tcp(" + dbConfig.MySQLConfig.Host + ":" + dbConfig.MySQLConfig.Port + ")/" + dbConfig.MySQLConfig.Database + "?charset=utf8mb4&parseTime=True" db, err = sqlx.Open("mysql", dsn) if err != nil { panic("Failed to connect database, please check the configuration.") } db.SetMaxOpenConns(30) db.SetMaxIdleConns(15) }