正在为 Go 中的 MySQL 到 JSON 组装一个快速转储器。但是我发现我从数据库中检索的所有内容都是一个[]byte数组。因此,我将所有内容都编码为字符串,而不是原生 JSON 整数或布尔值。代码的子集:import ( "encoding/json" "database/sql" _ "github.com/go-sql-driver/mysql")func dumpTable(w io.Writer, table) { // ... rows, err := Query(db, fmt.Sprintf("SELECT * FROM %s", table)) checkError(err) columns, err := rows.Columns() checkError(err) scanArgs := make([]interface{}, len(columns)) values := make([]interface{}, len(columns)) for i := range values { scanArgs[i] = &values[i] } for rows.Next() { err = rows.Scan(scanArgs...) checkError(err) record := make(map[string]interface{}) for i, col := range values { if col != nil { fmt.Printf("\n%s: type= %s\n", columns[i], reflect.TypeOf(col)) switch t := col.(type) { default: fmt.Printf("Unexpected type %T\n", t) case bool: fmt.Printf("bool\n") record[columns[i]] = col.(bool) case int: fmt.Printf("int\n") record[columns[i]] = col.(int) case int64: fmt.Printf("int64\n") record[columns[i]] = col.(int64) case float64: fmt.Printf("float64\n") record[columns[i]] = col.(float64) case string: fmt.Printf("string\n") record[columns[i]] = col.(string) case []byte: // -- all cases go HERE! fmt.Printf("[]byte\n") record[columns[i]] = string(col.([]byte)) case time.Time: // record[columns[i]] = col.(string) } } } s, _ := json.Marshal(record) w.Write(s) io.WriteString(w, "\n") }}
3 回答
- 3 回答
- 0 关注
- 503 浏览
添加回答
举报
0/150
提交
取消