diff options
author | Rob Pike <r@golang.org> | 2011-12-02 00:02:24 -0800 |
---|---|---|
committer | Rob Pike <r@golang.org> | 2011-12-02 00:02:24 -0800 |
commit | 30775f67e7d5e897d4d9aafe8ab84a5f65550ce4 (patch) | |
tree | c3ce5fd1902459c59c4e62da546e4a8dfc3a2f0e | |
parent | 8bc641083709c33a8173e719d8bcdd6b35ac5925 (diff) | |
download | go-30775f67e7d5e897d4d9aafe8ab84a5f65550ce4.tar.gz go-30775f67e7d5e897d4d9aafe8ab84a5f65550ce4.zip |
encoding/gob: don't send type info for unexported fields
Fixes #2517.
R=golang-dev, dsymonds
CC=golang-dev
https://golang.org/cl/5440079
-rw-r--r-- | src/pkg/encoding/gob/encoder.go | 4 | ||||
-rw-r--r-- | src/pkg/encoding/gob/encoder_test.go | 16 |
2 files changed, 19 insertions, 1 deletions
diff --git a/src/pkg/encoding/gob/encoder.go b/src/pkg/encoding/gob/encoder.go index e4a48dfc4f..a15b5a1f9a 100644 --- a/src/pkg/encoding/gob/encoder.go +++ b/src/pkg/encoding/gob/encoder.go @@ -119,7 +119,9 @@ func (enc *Encoder) sendActualType(w io.Writer, state *encoderState, ut *userTyp switch st := actual; st.Kind() { case reflect.Struct: for i := 0; i < st.NumField(); i++ { - enc.sendType(w, state, st.Field(i).Type) + if isExported(st.Field(i).Name) { + enc.sendType(w, state, st.Field(i).Type) + } } case reflect.Array, reflect.Slice: enc.sendType(w, state, st.Elem()) diff --git a/src/pkg/encoding/gob/encoder_test.go b/src/pkg/encoding/gob/encoder_test.go index bc5af120af..5bc957bb37 100644 --- a/src/pkg/encoding/gob/encoder_test.go +++ b/src/pkg/encoding/gob/encoder_test.go @@ -662,3 +662,19 @@ func TestSequentialDecoder(t *testing.T) { } } } + +// Should be able to have unrepresentable fields (chan, func) as long as they +// are unexported. +type Bug2 struct { + A int + b chan int +} + +func TestUnexportedChan(t *testing.T) { + b := Bug2{23, make(chan int)} + var stream bytes.Buffer + enc := NewEncoder(&stream) + if err := enc.Encode(b); err != nil { + t.Fatalf("error encoding unexported channel: %s", err) + } +} |