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

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

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

问题

当我使用&simplejson.Json{v}(v是从文件中读取的接口,其实际数据结构是map[string]interface{})时,显示此错误。详细信息如下:

一个名为abcd的json文件

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

而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()
}

当我执行它时,在这一行m := &sjson.Json{v}显示implicit assignment of unexported field 'data' in simplejson.Json literal

英文:

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. Details:

A json file named abcd

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

}

And the go file is

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}.

答案1

得分: 1

这行代码:

m := &sjson.Json{v}

试图创建一个来自go-simplejson包中Json结构类型的值(并取其地址)。类型声明如下:

type Json struct {
    data interface{}
}

它有一个字段:data,该字段未导出。这意味着除了go-simplejson包之外的其他包无法引用该字段。当你使用结构字面量&sjson.Json{v}时,它会尝试使用值v来初始化Json.data字段,这是不允许的。你不能这样做。

Json类型并不是为了让你指定内部的data,它设计成data将作为一些解码后的JSON数据的占位符(参见NewFromReader()NewJson()这样的构造函数)。

data字段由go-simplejson包内部处理,你不能自己设置它。你可以使用sjson.New()来获取一个新的*Json值,它将使用空映射(map[string]interface{})来初始化data字段。你也可以使用Json.Map()方法,该方法断言data是一个映射并以该方式返回:

js := sjson.New()
m, err := js.Map()
if err != nil {
    // 处理错误
}

// 现在你有了一个类型为map[string]interface{}的映射

或者,要填充Json内部的data,你可以使用其Json.Set()方法。

英文:

This line:

m := &sjson.Json{v}

Tries to create a value (and take the address) of the struct type Json from package go-simplejson. The type declaration is:

type Json struct {
    data interface{}
}

It has one field: data which is unexported. That means packages other than go-simplejson cannot refer to this field. 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. 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).

This data field is handled internally by the go-simplejson package, you cannot set it yourself. You may use sjson.New() to obtain a new *Json value which will initialize this data field with an empty map (map[string]interface{}). You may also use Json.Map() method which asserts that data is a map and returns it like that:

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 its Json.Set() method.

huangapple
  • 本文由 发表于 2016年4月1日 14:51:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/36349795.html
匿名

发表评论

匿名网友

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

确定