douyanpeng0748 2014-02-14 18:51
浏览 47

从另一个包扩展结构时非导出字段

I am using go-simplejson in a small toy project (acutally my first go project ever). In this project I need to extend the simplejson package:

simplejson introduces a small struct (with some helpful methods) with a constructor function:

type Json struct {
    data interface{}
}

// NewJson returns a pointer to a new `Json` object
// after unmarshaling `body` bytes
func NewJson(body []byte) (*Json, error) {
    j := new(Json)
    err := j.UnmarshalJSON(body)
    if err != nil {
        return nil, err
    }
    return j, nil
}

In my project, i want to sync some xml data to CouchDB. For this I would love to make a new constructor, where I deliver the decoded xml (type interface{}) directly to the Json object, then marshal it to JSON.

This is trivial:

func NewJsonFromData(data interface{}) *Json {
    return &Json{data}
}

now since this is some kind of "hack", I want to avoid forking go-simplejson and implement it in go-simplejson, because I would "pollute" the general package. I would like to do this in my own little toy package. And here is where te trouble begins:

the data field is unexported and not visible outside the package... so putting this function declaration in my other package like so:

func NewJsonFromData(data interface{}) *simplejson.Json {
    return &simplejson.Json{data}
}

doesn't work, it gives the following compile error:

implicit assignment of unexported field 'data' in simplejson.Json literal

also doing

type Json struct {
    simplejson.Json
}

in my own package doesn't work, i cannot set the data field in the constructor of my own Json struct that composes simplejson.Json, since its unexported (it would obviously work if the field would be called "Data".

making a file in my own package folder having

package simplejson

is not accepted, since go forces you to keep all the files of a package in one folder.

  1. Is it correct that it is simply impossible in go to extend package functionality if the developer decided to not export struct fields? (I see the reason why one would do that, on the other hand, I would like to do it, since its "only" a toy example :) )

  2. If it's possible, whats the best way to do it?

  • 写回答

1条回答 默认 最新

  • douya6229 2014-02-14 20:11
    关注

    This the same in all languages, private fields are not accessible outside their scope.

    That being said if you have the interface already why not use the standard JSON library which has an marshal function which takes an interface directly without having to set to a field.

    package main
    
    import (
        "encoding/json"
    )
    
    func main() {
        v := ....
        jsonBytes, err := json.Marshal(v)
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探