编码和解码结构

发布于 2025-01-20 03:42:11 字数 773 浏览 1 评论 0原文

我正在尝试编码和解码结构,我已经四处搜索了很多,并且有关此主题的许多问题通常是想要编码原始词或简单结构的人。我想要的是编码一个可以看起来像这样的结构:

    Name string           
    Id   int               
    file *os.File          
    keys *ecdsa.PrivateKey 
}

名称和ID没问题,我可以使用GOB或JSON编组对其进行编码。但是,当我想使用GOB编码文件时文件结构是较低的情况。我会使用这样的函数,

    buf := bytes.Buffer{}
    enc := gob.NewEncoder(&buf)
    gob.Register(big.Int{})
...
    err := enc.Encode(&p)
    if err != nil {
        log.Fatal(err)
    }
    fmt.Println("uncompressed size (bytes): ", len(buf.Bytes()))
    return buf.Bytes()
}

我不确定在编码函数中注册是否正确,但是我必须注册我要编码的一个特定结构的所有结构,这似乎很奇怪。例如,使用文件,我必须注册大量接口,这似乎不是正确的方法。是否有一种简单的方法来编码和解码具有更复杂性的结构。

如果我使用JSON编组来执行此操作,则如果我使用指向另一个结构的指针,它将始终返回无。有没有办法获取我想要的所有信息?

谢谢!

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:

    Name string           
    Id   int               
    file *os.File          
    keys *ecdsa.PrivateKey 
}

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 usegob.Register(os.File{}) 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

    buf := bytes.Buffer{}
    enc := gob.NewEncoder(&buf)
    gob.Register(big.Int{})
...
    err := enc.Encode(&p)
    if err != nil {
        log.Fatal(err)
    }
    fmt.Println("uncompressed size (bytes): ", len(buf.Bytes()))
    return buf.Bytes()
}

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.

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?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

殤城〤 2025-01-27 03:42:11

想象一下您的结构体连接到 /foo/bar/baz.txt 中的文件,然后序列化您的结构体。您将其发送到另一台计算机(可能在不同的操作系统中)并重新创建结构。你期望什么?

如果序列化、删除文件(或更新内容)并在同一台计算机中重新创建结构会怎样?

一种解决方案是存储文件的内容。

另一个解决方案是存储文件的路径,当您反序列化该结构时,您可以尝试重新打开该文件。您可以通过存储内容、大小和其他元数据的哈希来添加安全层,以检查文件是否相同。

答案将指导您实现最佳实施

Imagine your struct ponts to a file in /foo/bar/baz.txt and you serialize your struct. The you send it to another computer (perhaps in a different operational system) and re-create the struct. What do you expect?

What if you serialize, delete the file (or update the content) and re-create the struct in the same computer?

One solution is store the content of the file.

Another solution is to store the path to the file and, when you deserialize the struct you can try to reopen the file. You can add a security layer by storing the hash of the content, size and other metadata to check if the file is the same.

The answer will guide you to the best implementation

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文