Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8674531
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T19:43:59+00:00 2026-06-12T19:43:59+00:00

I already found encoding/binary package to deal with it, but it depended on reflect

  • 0

I already found encoding/binary package to deal with it, but it depended on reflect package so it didn’t work with uncapitalized(that is, unexported) struct fields. However I spent a week to find that problem out, I still have a question: if struct fields should not be exported, how do I dump them easily into binary data?

EDIT: Here’s the example. If you capitalize the name of fields of Data struct, that works properly. But Data struct was intended to be an abstract type, so I don’t want to export these fields.

package main
import (
    "fmt"
    "encoding/binary"
    "bytes"
)

type Data struct {
    id int32
    name [16]byte
}


func main() {
    d := Data{Id: 1}
    copy(d.Name[:], []byte("tree"))
    buffer := new(bytes.Buffer)
    binary.Write(buffer, binary.LittleEndian, d)
    // d was written properly
    fmt.Println(buffer.Bytes())
    // try to read...
    buffer = bytes.NewBuffer(buffer.Bytes())
    var e = new(Data)
    err := binary.Read(buffer, binary.LittleEndian, e)
    fmt.Println(e, err)
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T19:44:00+00:00Added an answer on June 12, 2026 at 7:44 pm

    Your best option would probably be to use the gob package and let your struct implement the GobDecoder and GobEncoder interfaces in order to serialize and deserialize private fields.

    This would be safe, platform independent, and efficient. And you have to add those GobEncode and GobDecode functions only on structs with unexported fields, which means you don’t clutter the rest of your code.

    func (d *Data) GobEncode() ([]byte, error) {
        w := new(bytes.Buffer)
        encoder := gob.NewEncoder(w)
        err := encoder.Encode(d.id)
        if err!=nil {
            return nil, err
        }
        err = encoder.Encode(d.name)
        if err!=nil {
            return nil, err
        }
        return w.Bytes(), nil
    }
    
    func (d *Data) GobDecode(buf []byte) error {
        r := bytes.NewBuffer(buf)
        decoder := gob.NewDecoder(r)
        err := decoder.Decode(&d.id)
        if err!=nil {
            return err
        }
        return decoder.Decode(&d.name)
    }
    
    func main() {
        d := Data{id: 7}
        copy(d.name[:], []byte("tree"))
        buffer := new(bytes.Buffer)
        // writing
        enc := gob.NewEncoder(buffer)
        err := enc.Encode(d)
        if err != nil {
            log.Fatal("encode error:", err)
        }
        // reading
        buffer = bytes.NewBuffer(buffer.Bytes())
        e := new(Data)
        dec := gob.NewDecoder(buffer)
        err = dec.Decode(e)
        fmt.Println(e, err)
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Have found already some people asking the same, but the solutions didn't work for
I've already found the following question , but I was wondering if there was
This is my first question, but I've already found this site extremely helpful, so
I found already about 5 answers, tried all of them and didn't get it
I have found already some similar discussion on this, but would like to investigate
I found that utf-8 is the standard , but Mysql doesn't fully support utf-8(4bytes)
already found something about this task but these snippets where just SQL Querys, is
Already found this page with some helpful hints. Problem is I need to debug
Has someone already found some tweaks to improve the compilation speed of Play 2.0?
I need an expression which matches,DateTime format (DD/MM/YYYY),i've already found it. However,it only works

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.