package main import ( "database/sql" "fmt" _ "github.com/go-sql-driver/mysql" ) var mysqlDb *sql.DB func regMysql(brand string) { //const mySqlUrl = "%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&loc=Local" url := "" switch brand { case "ugg": url = fmt.Sprintf("root:plmPLM0.@tcp(%s.imix7.com:13306)/uggwb?charset=utf8mb4&loc=Local", brand) case "hdugg": url = fmt.Sprintf("root:plmPLM0.@tcp(%s.imix7.com:13306)/hduggwb?charset=utf8mb4&loc=Local", brand) case "ecco": url = fmt.Sprintf("root:plmPLM0.@tcp(%s.imix7.com:13306)/weimob?charset=utf8mb4&loc=Local", brand) } if len(url) == 0 { panic("mysql brand error") } db, err := sql.Open("mysql", url) if err != nil { panic(err) } err = db.Ping() if err != nil { panic(err) } mysqlDb = db } type Vip struct { Wid int64 Phone string Name string } func GetAllWid() ([]Vip, error) { sqlStr := `select wid,phone,name from user order by wid ` rows, err := mysqlDb.Query(sqlStr) if err != nil { return nil, err } defer rows.Close() var vips []Vip for rows.Next() { var vip Vip err := rows.Scan(&vip.Wid, &vip.Phone, &vip.Name) if err != nil { return nil, err } vips = append(vips, vip) } return vips, nil }