Saving and Read byte[] in Golang from database

huangapple go评论78阅读模式
英文:

Saving and Read byte[] in Golang from database

问题

我正在使用Golang编写一个API,使用Gorm作为ORM。目前我遇到了一个关于[]byte字段的问题。我在结构体中定义如下:

type Member struct {
    MyField []byte `gorm:"column:MyField" schema:"-"`
}

然后,我有保存和读取该资源的方法。当我保存时,保存的值与读取的值不同。例如,我使用bcrypt生成哈希密码,尝试使用12345作为密码,结果如下:

保存时的值:

[36 50 97 36 49 48 36 46 56 98 88 72 82 71 113 66 100 65 105 103 70 119 114 97 73 77 99 78 117 106 54 78 103 88 68 49 56 110 103 112 105 86 104 79 117 47 114 57 116 51 47 53 97 100 109 103 106 46 68 109]

从数据库中读取并打印的值:

[36 50 97 36 48 52 36 56 49 67 66 121 118 90 47 47 104 49 83 120 50 108 112 71 73 51 67 88 46 97 52 74 54 66 84 73 106 105 110 122 98 69 90 51 78 113 67 66 49 103 50 56 116 47 57 120 78 103 109 54]

它们是不同的,为什么会这样?

我使用gorm创建了表,该列的类型在我的PostgreSQL数据库中定义为bytea

保存的代码如下:

bs, err := bcrypt.GenerateFromPassword([]byte(Pass), bcrypt.DefaultCost)
if err != nil {
    fmt.Println("Hey error!")
}
user.MyField = bs
db.Create(&user)

读取的代码如下:

db.First(&user, id)
fmt.Println(user.MyField)
英文:

I am working on a API in Golang, am using Gorm as ORM. Currently am having an issue with a []byte field, I have it defined in my struct as:

type Member struct {
    MyField              []byte     `gorm:"column:MyField" schema:"-"`
}

Then, I have the methods to Save and Read that resource, so the value when I save it is different to value that I am reading. For example,am using bcrypt to generate the hashed password, trying with 12345 the result is:

[36 50 97 36 49 48 36 46 56 98 88 72 82 71 113 66 100 65 105 103 70 119 114 97 73 77 99 78 117 106 54 78 103 88 68 49 56 110 103 112 105 86
104 79 117 47 114 57 116 51 47 53 97 100 109 103 106 46 68 109]

Until there everything is ok, then when I read the register from the database and print that value I get:

[36 50 97 36 48 52 36 56 49 67 66 121 118 90 47 47 104 49 83 120 50 108 112 71 73 51 67 88 46 97 52 74 54 66 84 73 106 105 110 122 98 69 90 51
78 113 67 66 49 103 50 56 116 47 57 120 78 103 109 54]

They are different, why?
I used gorm to create the tables, and the type of that column is defined in my postgres database as bytea

The code

To save:

bs, err := bcrypt.GenerateFromPassword([]byte(Pass), bcrypt.DefaultCost)	

if err != nil {
        fmt.Println("Hey error!")
}
user.MyField = bs
db.Create(&user)

To Read:
db.First(&user,id)
fmt.Println(user.MyField)

答案1

得分: 1

我猜你的问题可能是在db.First(&user,id)这一行出现了错误的id。你可能获取到了错误的行。我尝试复现了你的错误,但没有成功。以下代码可以正常工作:

bs, err := bcrypt.GenerateFromPassword([]byte("12345"), bcrypt.DefaultCost)
expected := bs

db.Create(&Member{MyField: bs})
var member Member

db.First(&member)
actual := member.MyField

if !bytes.Equal(actual, expected) {
    panic("字段不相等")
}
英文:

I suppose your problem is a wrong id at db.First(&user,id). You probably get the wrong row. I've tried to repoduce your error but couldn't. The following code works fine:

bs, err := bcrypt.GenerateFromPassword([]byte("12345"), bcrypt.DefaultCost)
expected := bs

db.Create(&Member{MyField: bs})
var member Member

db.First(&member)
actual := member.MyField

if !bytes.Equal(actual, expected) {
	panic("fields are not equal")
}

huangapple
  • 本文由 发表于 2017年6月14日 22:12:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/44547272.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定