import (
"database/sql"
"encoding/json"
"fmt"
_ "github.com/go-sql-driver/mysql"
)
type User struct {
Name string `json:name`
Picture []uint8 `json:picture`
}
func main(){
//straight to the query
rows, err := 'SELECT name, picture FROM ms_users' // picture is longblob type in database
checkErr(err)
var usr User
for rows.Next(){
err = rows.Scan(&usr.Name, &usr.Picture)
checkErr(err)
}
jsn, err := json.Marshal(usr)
fmt.Printf("%v, "string(jsn))
}
使用上述代码,我只能获取名称值,但图片为空。如何将数据库中的blob值存储到结构体中? 任何答案都将不胜感激!谢谢!
[]byte
代替[]uint8
。在此处列出了[]byte
,但未列出[]uint8
。https://golang.org/pkg/database/sql/#Scan - RayfenWindspearstring
https://github.com/go-sql-driver/mysql/blob/336127359b09c9c8164e39c4c44f4d36ef081dc8/driver_test.go#L972 - mkopriva[]byte
无法工作时,string
却可以。在内部,string
就是一个[]byte
。 - RayfenWindspear