Ignore Fields like channel And Func While Encoding

Problem Description

I wanted to Encode a complex structure ( it is a state of game play, have lots of fields( both exported and un exported) and can have channels and Func type) . when there is a failure in server i will Encode the entire data using “encoding/gob” package and will put it in data base . later i need to decode the entire state as it is. but gob will not support chan or func data types.

i really needed that fields to get ignored or some other way to do this?

it would be really appreciated if some one can help

1) is there any way that we can ignore the fields that have types (chan or Func) because i don’t want to store them
2) is there any other way to solve this problem

one problem that i was solved is , considering the un exported fields

this code is from “encoding/gob”

func isSent(field *reflect.StructField) bool {
if !isExported(field.Name) {
	return false
}
// If the field is a chan or func or pointer thereto, don't send it.
// That is, treat it like an unexported field.
typ := field.Type
for typ.Kind() == reflect.Ptr {
	typ = typ.Elem()
}
if typ.Kind() == reflect.Chan || typ.Kind() == reflect.Func {
	return false
}
return true

}

i removed isExported check , so for now the un exported fields are also working fine while encoding

but the above problem still exist?

thanks in advance
Junaid

I did not try this myself but the documentation says

The source and destination values/types need not correspond exactly. For structs, fields (identified by name) that are in the source but absent from the receiving variable will be ignored. Fields that are in the receiving variable but missing from the transmitted type or value will be ignored in the destination.

What exactly is your problem?

@lutzhorn thank you for reply,

i really want to encode a struct with some good amount of fields ( can be maps , pointer to another struct, etc) for storing and recovering , but “gob/encoding” not supporting un exported filed’s , and i found another package

https://github.com/kelindar/binary

that support un exported filed’s for encoding but it has some limitation with pointers

is there any package out there i can use for encoding and storing and later decode without exporting the fileds in the strcut ?

i know this is possible by package “reflect” , thinking of writing custom encoder / decoder for encoding and decoding state without exporting the fields in the struct . but i think it will take some time to do it .

the structure of the data is always known

but as mentioned in the top

 if !isExported(field.Name) {
return false

}

commenting this line in package “encoding/gob” will not completely solve the problem.

eg:it helping to decode encode and decode some un exported fields
but failing on un exported time.Time filed

please suggest a way to solve this problem , i don’t want to create another struct and converting to and forth or exporting the fileds,

thanks is advance,
Junaid