Exported and Unexported fields in Go Language

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

Exported and Unexported fields in Go Language

问题

我有一个在Go中的函数,希望使用gob对返回值进行编码。返回值是一个结构体指针。然而,尽管我理解什么是导出变量,但我不太确定如何使其工作。

以下是我的函数示例:

func loadXYZ(root *structABC) *structABC {
    const once = "stateData.bin"
    rd, err := ioutil.ReadFile(once)
    if err != nil {
        // 进行一些计算并存储在 "root" 中
        buf := &bytes.Buffer{}
        err = gob.NewEncoder(buf).Encode(root)
        if err != nil {
            panic(err)
        }
        err = ioutil.WriteFile(once, buf.Bytes(), 0666)
        if err != nil {
            panic(err)
        }
        return root
    }
    var d *structABC
    err = gob.NewDecoder(bytes.NewReader(rd)).Decode(&d)
    if err != nil {
        panic(err)
    }
    return d
}

这是我得到的错误信息:

panic: gob: type main.stateNode has no exported fields

我知道为什么会出现这个错误,但有人可以帮我解决吗?

英文:

I have a function in Go, the return value of which I hope to encode using gob. The return value is a struct pointer. However even though I do understand what exported variables are, I am not quite sure how to get it working.

Here is what my function is like

fun loadXYZ(root *structABC) *structABC{
    const once = "stateData.bin"
    rd, errr := ioutil.ReadFile(once)
    if errr!=nil{

        //Do some computation and store in "root"

        buf := &bytes.Buffer{}
	    errr = gob.NewEncoder(buf).Encode(root)
	    if errr != nil {
		    panic(errr)
	    }
	    errr = ioutil.WriteFile(once, buf.Bytes(), 0666)
	    if errr != nil {
		    panic(errr)
	    }
	    return root
    }
    var d *structABC
    errr = gob.NewDecoder(bytes.NewReader(rd)).Decode(&d)
    if errr != nil {
	     panic(errr)
    }
    return d
}

This is the error I get

panic: gob: type main.stateNode has no exported fields

I know why the error is occurring. But can someone help me solve it?

答案1

得分: 33

在Go语言中,以大写字母开头的字段和变量被称为"导出的",可以被其他包访问。以小写字母开头的字段被称为"未导出的",只能在自己所在的包内部访问。

encoding/gob包依赖反射来编码值,只能看到"导出的"结构体字段。

为了使字段可以被编码,需要将stateNode结构体中希望保存的字段名的首字母大写。

英文:

In go, fields and variables that start with an Uppercase letter are "Exported", and are visible to other packages. Fields that start with a lowercase letter are "unexported", and are only visible inside their own package.

The encoding/gob package depends on reflection to encode values, and can only see exported struct fields.

In order to make things encodable, capitalize the first letter of each field name in your stateNode struct that you want to be saved.

答案2

得分: 9

导出字段是以大写字母开头的字段,例如:

type stateNode struct {
    ImExported string // 导出字段
    butImNot string // 非导出字段
}
英文:

Exported field it's a filed which name started with capitalized char like:

type stateNode struct {
    ImExported string // Exported
    butImNot string // unexported
}

huangapple
  • 本文由 发表于 2016年10月26日 15:26:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/40256161.html
匿名

发表评论

匿名网友

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

确定