简体   繁体   English

使用 bitly 的 go-simplejson 时,在 simplejson.Json 文字中显示未导出字段“数据”的隐式分配

[英]Show implicit assignment of unexported field 'data' in simplejson.Json literal when use bitly's go-simplejson

When I use like &simplejson.Json{v} (v is a interface read from file and it's actual data structure is map[string]interface{}), then show this error.当我使用 like &simplejson.Json{v} (v 是从文件中读取的接口,它的实际数据结构是 map[string]interface{}),然后显示此错误。 Details:细节:

A json file named abcd一个名为abcd的 json 文件

{
    "pids": [
        { 
            "pid": 168043, 
            "target_regions": [
                40, 
                25, 
                43, 
                299, 
                240
            ]
         },
        { 
            "pid": 168044, 
            "target_regions": [
                63, 
                65,
                68
            ]
        }
    ]

} }

And the go file is go文件是

package main    

import (
    "fmt"
    "io/ioutil"    

    sjson "github.com/bitly/go-simplejson"
)    

type pidInfo struct {
    Pid           uint64   `json:"pid"`
    TargetRegions []uint32 `json:"target_regions"`
}    

type pidUnitInfo struct {
    Pid2Info map[uint64]*pidInfo
}    

func build() error {
    content, _ := ioutil.ReadFile("./abcd")
    json, err := sjson.NewJson(content)
    if err != nil {
        return err
    }
    newPidUnitInfo(json)
    return nil
}    

func newPidUnitInfo(json *sjson.Json) (*pidUnitInfo, error) {
    newInfo := new(pidUnitInfo)
    newInfo.buildPid2Info(json)
    return nil, nil
}    

func (pui *pidUnitInfo) buildPid2Info(json *sjson.Json) error {
    raw, ok := json.CheckGet("pids")
    if !ok {
        return fmt.Errorf("not found json key %v", "pids")
    }
    pids, err := raw.Array()
    if err != nil {
        return err
    }
    pui.Pid2Info = make(map[uint64]*pidInfo, len(pids))
    for _, v := range pids {
        fmt.Println(v)
        m := &sjson.Json{v}
        fmt.Println(m)
    }
    return nil
}    

func main() {
    build()
}

When I execute it, show implicit assignment of unexported field 'data' in simplejson.Json literal at this line m := &sjson.Json{v} .当我执行它时,在这行m := &sjson.Json{v}显示implicit assignment of unexported field 'data' in simplejson.Json literal

This line:这一行:

m := &sjson.Json{v}

Tries to create a value (and take the address) of the struct type Json from package go-simplejson .尝试从go-simplejson包中创建结构类型Json的值(并获取地址)。 The type declaration is:类型声明是:

type Json struct {
    data interface{}
}

It has one field: data which is unexported.它只有一个字段:未导出的data That means packages other than go-simplejson cannot refer to this field.这意味着除了go-simplejson之外的包不能引用这个字段。 When you use a struct literal &sjson.Json{v} , it would try to initialize the Json.data field with value v which is a violation of this.当您使用结构文字&sjson.Json{v} ,它会尝试使用值v初始化Json.data字段,这违反了这一点。 You cannot do this.你不可以做这个。

The Json type is not designed for you to specify the internal data , it is designed so that the data will be the placeholder of some decoded JSON data (see the NewFromReader() and NewJson() constructor-like functions). Json类型不是为您指定内部data而设计的,它的设计目的是使data成为某些已解码 JSON 数据的占位符(请参阅NewFromReader()NewJson()类似构造函数的函数)。

This data field is handled internally by the go-simplejson package, you cannot set it yourself.data字段由go-simplejson包内部处理,您无法自行设置。 You may use sjson.New() to obtain a new *Json value which will initialize this data field with an empty map ( map[string]interface{} ).您可以使用sjson.New()获取新的*Json值,该值将使用空映射 ( map[string]interface{} ) 初始化此data字段。 You may also useJson.Map() method which asserts that data is a map and returns it like that:您还可以使用Json.Map()方法,该方法断言data是一个地图并像这样返回它:

js := sjson.New()
m, err := js.Map()
if err != nil {
    // Handle error
}

// Now you have a map of type map[string]interface{}

Or to populate the data inside a Json , you can use itsJson.Set() method.或者要在Json填充data ,您可以使用其Json.Set()方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM