title: Encoding and decoding structs of
body: <p>I'm trying to encode and decode structs, I've searched around quite a bit and a lot of the questions regarding this topic is usually people who want to encode primitives, or simple structs. What I want is to encode a struct that could look like this:</p>
  <pre><code>    Name string           
      Id   int               
      file *os.File          
      keys *ecdsa.PrivateKey 
  }
  </code></pre>
  <p>The name and the ID is no problem, and I can encode them using either gob or json marshalling. However when I want to encode a file for example using gob, I'd use<code>gob.Register(os.File{})</code> I get an error that file has no exported fields, due to the fields in the file struct being lower case. I would use a function like this</p>
  <pre><code>    buf := bytes.Buffer{}
      enc := gob.NewEncoder(&amp;buf)
      gob.Register(big.Int{})
  ...
      err := enc.Encode(&amp;p)
      if err != nil {
          log.Fatal(err)
      }
      fmt.Println(&quot;uncompressed size (bytes): &quot;, len(buf.Bytes()))
      return buf.Bytes()
  }
  </code></pre>
  <p>I'm not sure if it's correct to register within the encode function, however it seems odd that I have to register all structs that is being referenced to for the one specific struct i want to encode. For example with a file, I would have to register a ton of interfaces, it doesn't seem to be the correct way to do it. Is there a simple way to encode and decode structs that have a bit more complexity.</p>
  <p>If I use json marshalling to do this it will always return nil if I use a pointer to another struct. Is there a way to get all the information I want?</p>
  <p>Thanks!</p>