Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

encoding/gob: no way to skip marshalling exported struct field, docs unclear that a struct must have minimum one exported field #19969

willauld opened this issue Apr 14, 2017 · 6 comments


Copy link

@willauld willauld commented Apr 14, 2017

Please answer these questions before submitting your issue. Thanks!

What version of Go are you using (go version)?


What operating system and processor architecture are you using (go env)?

go env

set GOARCH=amd64
set GOBIN=
set GOEXE=.exe
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOOS=windows
set GOPATH=C:\home\auld\godev
set GOROOT=C:\Go
set GOTOOLDIR=C:\Go\pkg\tool\windows_amd64
set GCCGO=gccgo
set CC=gcc
set GOGCCFLAGS=-m64 -mthreads -fmessage-length=0 -fdebug-prefix-map=C:\Use
rs\auld\AppData\Local\Temp\go-build485133467=/tmp/go-build -gno-record-gcc
set CXX=g++
set PKG_CONFIG=pkg-config
set CGO_CFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2

What did you do?

The program should transmit its User struct and receive it via a fake network and print the transferred data. Line 15-17 of the below play area program contains three cases. The third case (3) works correctly and transfers all the data as expected. Case two (2) also transfers all the data but based on the documentation I believe only the values associated with exported field should be transferred. Case one (1) does not transfer any data, not even the exported fields from User.

I believe both case 1 and 2 represent bugs.

If possible, provide a recipe for reproducing the error.
A complete runnable program is good.
A link on is best.

What did you expect to see?

Case 1: &{Donald DuckPass []}
Case 2: &{Donald DuckPass [{ 2} { 1}]}
Case 3: &{Donald DuckPass [{1 2} {2 1}]}

What did you see instead?

Case 1: 2009/11/10 23:00:00 encode error:gob: type main.j has no exported fields
Case 2: &{Donald DuckPass [{1 2} {2 1}]}
Case 3: &{Donald DuckPass [{1 2} {2 1}]}

Copy link

@bradfitz bradfitz commented Apr 14, 2017

Dup of #19970?

The docs say:

"Structs encode and decode only exported fields."

I think this is working as intended. Let me know if I don't understand.

@bradfitz bradfitz closed this Apr 14, 2017
Copy link

@willauld willauld commented Apr 14, 2017

Case 2 encodes/decode j.a which is not exported. This contradicts your quote from the document.

In Case 1 it is NOT encoding/decoding User.Name, User.Pass which I believe also contradicts the quote. In addition I believe User.List should be encoded even though it would be empty.

Copy link

@bradfitz bradfitz commented Apr 15, 2017

Okay, I'll let @robpike decide or document this.

Rob, it seems that @willauld wants to be able to mark a field as not marshalled, and/or document that marshalling a struct with zero exported fields is an error. Currently the docs don't say a struct must have at minimum one exported field.

@bradfitz bradfitz reopened this Apr 15, 2017
@bradfitz bradfitz changed the title Gob package is not handling non-exported fields in array of struct correctly encoding/gob: no way to skip marshalling exported struct field, docs unclear that a struct must have minimum one exported field Apr 15, 2017
@bradfitz bradfitz added this to the Unplanned milestone Apr 15, 2017
Copy link

@rsc rsc commented Jun 5, 2017

ping @robpike

Copy link

@iWdGo iWdGo commented Jun 12, 2018

#5819 reports the same issue and a fix has been submitted. If accepted, documentation may remain as it is.

Copy link

@robpike robpike commented Jun 12, 2018

Duplicate of #5819.

@robpike robpike closed this Jun 12, 2018
@golang golang locked and limited conversation to collaborators Jun 12, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
6 participants
You can’t perform that action at this time.