1
0
mirror of https://github.com/golang/go synced 2024-11-24 10:10:07 -07:00

encoding/gob: don't send type info for unexported fields

Fixes #2517.

R=golang-dev, dsymonds
CC=golang-dev
https://golang.org/cl/5440079
This commit is contained in:
Rob Pike 2011-12-02 00:02:24 -08:00
parent 8bc6410837
commit 30775f67e7
2 changed files with 19 additions and 1 deletions

View File

@ -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())

View File

@ -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)
}
}