我用的github.com/go-sql-driver/mysql这个驱动,我像下面这样写没有报错,但是里面的数据为空或者0typedatastruct{idintnamestringweightint}rows,_:=db.Query("select*fromtestwhereid=?",2)forrows.Next(){line:=data{}err=rows.Scan(&line)log.Println(line)}运行结果2017/07/1713:37:50{00}存到map的代码rows,_:=db.Query("select*fromtestwhereid=?",2)columns,_:=rows.Columns()length:=len(columns)forrows.Next(){value:=make([]interface{},length)columnPointers:=make([]interface{},length)fori:=0;icolumnPointers[i]=&value[i] }rows.Scan(columnPointers...)data:=make(map[string]interface{})fori:=0;icolumnName:=columns[i] columnValue:=columnPointers[i].(*interface{})data[columnName]=*columnValue}log.Print(data)}有效果,但是看不懂为何要这样写
相关分类