- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
使用嵌入了 json 的结构会出现奇怪的行为。
package main
import (
"database/sql"
"encoding/json"
"fmt"
_ "github.com/lib/pq"
)
type Article struct {
Id int
Doc *json.RawMessage
}
func main() {
db, err := sql.Open("postgres", "postgres://localhost/json_test?sslmode=disable")
if err != nil {
panic(err)
}
_, err = db.Query(`create table if not exists articles (id serial primary key, doc json)`)
if err != nil {
panic(err)
}
_, err = db.Query(`truncate articles`)
if err != nil {
panic(err)
}
docs := []string{
`{"type":"event1"}`,
`{"type":"event2"}`,
}
for _, doc := range docs {
_, err = db.Query(`insert into articles ("doc") values ($1)`, doc)
if err != nil {
panic(err)
}
}
rows, err := db.Query(`select id, doc from articles`)
if err != nil {
panic(err)
}
articles := make([]Article, 0)
for rows.Next() {
var a Article
err := rows.Scan(
&a.Id,
&a.Doc,
)
if err != nil {
panic(err)
}
articles = append(articles, a)
fmt.Println("scan", string(*a.Doc), len(*a.Doc))
}
fmt.Println()
for _, a := range articles {
fmt.Println("loop", string(*a.Doc), len(*a.Doc))
}
}
输出:
scan {"type":"event1"} 17
scan {"type":"event2"} 17
loop {"type":"event2"} 17
loop {"type":"event2"} 17
因此文章最终指向相同的 json。
我做错了什么吗?
更新
编辑为可运行的示例。我正在使用 Postgres 和 lib/pq
。
最佳答案
我遇到了同样的问题,看了很长时间后,我阅读了 Scan 上的文档,上面写着
If an argument has type *[]byte, Scan saves in that argument a copy of the corresponding data. The copy is owned by the caller and can be modified and held indefinitely. The copy can be avoided by using an argument of type *RawBytes instead; see the documentation for RawBytes for restrictions on its use.
我认为如果您使用 *json.RawMessage 会发生什么,然后 Scan 不会将其视为 *[] 字节并且不会复制到其中。所以你在下一个循环扫描覆盖时进入内部 slice 。
更改您的 Scan 以将 *json.RawMessage 转换为 *[]byte,以便 Scan 将值复制到它。
err := rows.Scan(
&a.Id,
(*[]byte)(a.Doc),
)
关于sql - 来自数据库/sql json 列的 json.RawMessage 被覆盖,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24069459/
[]json.Rawmessage 是什么意思。它在这个结构中: type Request struct { Jsonrpc string `json:"jsonrpc"
尝试将结构转换为 []json.RawMessage。我的理解是 json.Marshal() 将它转换为 byte[],就像 []json.RawMessage 一样。然而,我似乎无法在两者之间转换
我在 golang 中有字节数组: obj_data, _ := json.Marshal(obj) 并且想要将这个字节数组设置为 json.RawMessage我认为它会起作用: data := j
本节Interface checks from Effective Go推荐 var _ json.Marshaler = (*RawMessage)(nil) 作为编译时检查 RawMessage
背景 我正在处理必须为 non-repudiable 的 JSON 数据. 授予我这些数据的 API 也有一个服务来验证数据最初来自他们。 As best as I can tell ,他们通过要求他
我正在使用 python boto 2.8,但无法删除消息。这是我的测试代码: conn = boto.sqs.connect_to_region("us-east-1",
是否可以在 Golang 中创建 json.RawMessage 文字? 我希望能够做这样的事情: type ErrorMessage struct { Timestamp string
我正在尝试创建一个方法扩展来检查我的结构是否已初始化,但我收到此错误: invalid operation: myStruct literal == inStruct (struct containi
这是对 JSON sometimes array sometimes object 的跟进 在最初的问题中,我询问了如何处理:“我正在使用一个可能为变量返回字符串或可能为变量返回数组的 json AP
请在此处找到代码 http://play.golang.org/p/zdQ14ItNBZ 我将 JSON 数据保存为 RawMessage,但无法将其解码。我需要对包含结构进行编码和解码,但我希望仍然
我有这个结构 type SyncInfo struct { Target string } 现在我从 ElasticSearch 查询一些 json 数据。 Source 是 json.Raw
我无法思考如何解码来自先前解码的 json 字节数组的原始 json 字符串。我有当前代码: type Message struct { Event string Data json.
在下面的示例中,我尝试使用反射将 Unmarshal 一个 json.RawMessage 放入一个 slice 中,以确定 json 中的项目类型.RawMessage,json.RawMessag
我需要解码可能具有以下格式的 json 对象: 格式一: { "contactType": 2, "value": "0123456789" } 格式2: { "contact
我运行以下代码: package main import ( "encoding/json" "fmt" ) func main() { raw := json.RawMess
尝试编码此结构时出现以下错误 json: error calling MarshalJSON for type json.RawMessage: unexpected end of JSON inpu
几周前,我开始学习 go 并尝试在学习基础知识的同时构建一个简单的博客应用程序。 目前我正在尝试使用 database/sql 和 github.com/lib/pq 包来获取和保存博客文章。我不喜欢
使用嵌入了 json 的结构会出现奇怪的行为。 package main import ( "database/sql" "encoding/json" "fmt" _
我想解码 var j = []byte(`[{"major":1},{"minor":0}]`) 进入 type Version struct { Major int `json:"major
我有一个: type User struct { UID int `json:"id"` FirstName
我是一名优秀的程序员,十分优秀!