在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
package main import ( "database/sql" "fmt" _ "github.com/go-sql-driver/mysql" ) func main() { db,err := sql.Open("mysql","root:1234@tcp(127.0.0.1:3306)/oa?charset=utf8"); if err != nil{ fmt.Printf("connect mysql fail ! [%s]",err) }else{ fmt.Println("connect to mysql success") } rows,err := db.Query("select id,username from user_info"); if err != nil{ fmt.Printf("select fail [%s]",err) } var mapUser map[string]int mapUser = make(map[string]int) for rows.Next(){ var id int var username string rows.Columns() err := rows.Scan(&id,&username) if err != nil{ fmt.Printf("get user info error [%s]",err) } mapUser[username] = id } for k,v := range mapUser{ fmt.Println(k,v); } }
|
请发表评论