How to encode struct to byte slice and decode byte slice back to original struct using gob encoding?

huangapple go评论79阅读模式
英文:

How to encode struct to byte slice and decode byte slice back to original struct using gob encoding?

问题

你好!以下是你提供的代码的翻译版本:

package main

import (
	"bytes"
	"encoding/gob"
	"fmt"
)

type object struct {
	name string
	age  int
}

func main() {
	inputObject := object{age: 22, name: "Zloy"}
	fmt.Println(inputObject)

	var inputBuffer bytes.Buffer
	gob.NewEncoder(&inputBuffer).Encode(inputObject)
	fmt.Println(inputBuffer)

	destBytes := inputBuffer.Bytes()
	fmt.Println("\n", destBytes, "\n")

	var outputBuffer bytes.Buffer
	outputBuffer.Write(destBytes)
	fmt.Println(outputBuffer)

	var outputObject object
	gob.NewDecoder(&outputBuffer).Decode(&outputObject)
	fmt.Println(outputObject)
}

输出结果:

{Zloy 22}
{[18 255 129 3 1 1 6 111 98 106 101 99 116 1 255 130 0 0 0] 0 0}

 [18 255 129 3 1 1 6 111 98 106 101 99 116 1 255 130 0 0 0]     

{[18 255 129 3 1 1 6 111 98 106 101 99 116 1 255 130 0 0 0] 0 0}
{ 0}

期望的输出结果:

{Zloy 22}
{[18 255 129 3 1 1 6 111 98 106 101 99 116 1 255 130 0 0 0] 0 0}

 [18 255 129 3 1 1 6 111 98 106 101 99 116 1 255 130 0 0 0]     

{[18 255 129 3 1 1 6 111 98 106 101 99 116 1 255 130 0 0 0] 0 0}
{Zloy 22}

你的代码看起来没有问题,输出结果也与预期一致。如果你有其他问题,请告诉我。

英文:

i am trying marshall go struct to bytes (via gob encoding), and then to unmarshall those bytes back to original object. I am getting unexpected result (object is not getting the correct values). Help me to correct the programm please.

Input:

package main

import (
	"bytes"
	"encoding/gob"
	"fmt"
)

type object struct {
	name string
	age  int
}

func main() {
	inputObject := object{age: 22, name: "Zloy"}
	fmt.Println(inputObject)

	var inputBuffer bytes.Buffer
	gob.NewEncoder(&inputBuffer).Encode(inputObject)
	fmt.Println(inputBuffer)

	destBytes := inputBuffer.Bytes()
	fmt.Println("\n", destBytes, "\n")

	var outputBuffer bytes.Buffer
	outputBuffer.Write(destBytes)
	fmt.Println(outputBuffer)

	var outputObject object
	gob.NewDecoder(&outputBuffer).Decode(&outputObject)
	fmt.Println(outputObject)
}

Output:

{Zloy 22}
{[18 255 129 3 1 1 6 111 98 106 101 99 116 1 255 130 0 0 0] 0 0}

 [18 255 129 3 1 1 6 111 98 106 101 99 116 1 255 130 0 0 0]     

{[18 255 129 3 1 1 6 111 98 106 101 99 116 1 255 130 0 0 0] 0 0}
{ 0}

Expected Output:

{Zloy 22}
{[18 255 129 3 1 1 6 111 98 106 101 99 116 1 255 130 0 0 0] 0 0}

 [18 255 129 3 1 1 6 111 98 106 101 99 116 1 255 130 0 0 0]     

{[18 255 129 3 1 1 6 111 98 106 101 99 116 1 255 130 0 0 0] 0 0}
{Zloy 22}

答案1

得分: 2

你需要将字段名称大写,以便可以公开导出/导入它们:

type object struct {
    Name string
    Age  int
}

链接:https://play.golang.org/p/_YqSmeDi6oH

英文:

You need to capitalize the field names to make them publicly exportable/importable:

type object struct {
    Name string
    Age  int
}

https://play.golang.org/p/_YqSmeDi6oH

huangapple
  • 本文由 发表于 2021年5月23日 19:59:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/67659494.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定