在Go语言中编组多维数组

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

Marshalling multidimensional array in Go

问题

以下是要翻译的内容:

代码如下,但简化版本是我有一个包含uint8类型的多维数组(切片)的结构体。我尝试创建一个方法,可以将任何结构体转储到文件中,但内部数组的情况...嗯,说实话我不太确定。以下是一个更大的代码库的简洁版本,但可以说明问题。我对Go相对较新,所以可能是我对类型转换的某些事情不知情。

package main

import (
	"encoding/json"
	"fmt"
	"io/ioutil"
)

type MyStruct struct {
	MyArray [][]uint8 `json:"arr"`
	Name    string    `json:"name"`
}

func CreateMyStruct(name string, size uint16, fill uint8) *MyStruct {
	var m [][]uint8
	var i, j uint16
	for i = 0; i < size; i++ {
		sl := []uint8{}
		for j = 0; j < size; j++ {
			sl = append(sl, fill)
		}
		m = append(m, sl)
	}

	return &MyStruct{
		MyArray: m,
		Name:    name,
	}
}

func main() {
	myStruct := CreateMyStruct("bobby", 4, 1)
	fmt.Printf("%+v\n", myStruct)
	str, _ := json.Marshal(myStruct)
	fmt.Printf("%+v\n", str)
	ioutil.WriteFile("my.json", str, 0644)
}

输出结果为:

$ go run test.go
&{MyArray:[[1 1 1 1] [1 1 1 1] [1 1 1 1] [1 1 1 1]] Name:bobby}
[123 34 97 114 114 34 58 91 34 65 81 69 66 65 81 61 61 34 44 34 65 81 69 66 65 81 61 61 34 44 34 65 81 69 66 65 81 61 61 34 44 34 65 81 69 66 65 81 61 61 34 93 44 34 110 97 109 101 34 58 34 98 111 98 98 121 34 125]
$ cat my.json
{"arr":["AQEBAQ==","AQEBAQ==","AQEBAQ==","AQEBAQ=="],"name":"bobby"}

显然,我期望的是:

[[1,1,1,1]] // 等等
英文:

Code below, but the short version is I have a multidimensional array (slice) of uint8 in a struct. I tried to create a method that I could dump any struct into and it would write to a file, but the inner arrays are...well I'm not sure to be honest. The following is a succinct version of a larger code base but gets the point across. I am relatively new to Go so maybe its something about type conversions I'm just unaware of.

package main

import (
	&quot;encoding/json&quot;
	&quot;fmt&quot;
	&quot;io/ioutil&quot;
)

type MyStruct struct {
	MyArray [][]uint8 `json:&quot;arr&quot;`
	Name    string    `json:&quot;name&quot;`
}

func CreateMyStruct(name string, size uint16, fill uint8) *MyStruct {
	var m [][]uint8
	var i, j uint16
	for i = 0; i &lt; size; i++ {
		sl := []uint8{}
		for j = 0; j &lt; size; j++ {
			sl = append(sl, fill)
		}
		m = append(m, sl)
	}

	return &amp;MyStruct{
		MyArray: m,
		Name:    name,
	}
}

func main() {
	myStruct := CreateMyStruct(&quot;bobby&quot;, 4, 1)
	fmt.Printf(&quot;%+v\n&quot;, myStruct)
	str, _ := json.Marshal(myStruct)
	fmt.Printf(&quot;%+v\n&quot;, str)
	ioutil.WriteFile(&quot;my.json&quot;, str, 0644)
}

The output being:

$ go run test.go
&amp;{MyArray:[[1 1 1 1] [1 1 1 1] [1 1 1 1] [1 1 1 1]] Name:bobby}
[123 34 97 114 114 34 58 91 34 65 81 69 66 65 81 61 61 34 44 34 65 81 69 66 65 81 61 61 34 44 34 65 81 69 66 65 81 61 61 34 44 34 65 81 69 66 65 81 61 61 34 93 44 34 110 97 109 101 34 58 34 98 111 98 98 121 34 125]
$ cat my.json
{&quot;arr&quot;:[&quot;AQEBAQ==&quot;,&quot;AQEBAQ==&quot;,&quot;AQEBAQ==&quot;,&quot;AQEBAQ==&quot;],&quot;name&quot;:&quot;bobby&quot;}

Obviously, I'm expecting something like:

[[1,1,1,1]] // etc

答案1

得分: 2

在Golang中,[]byte[]uint8的别名,而json.Marshall[]byte编码为base64编码的字符串(正如你所看到的):

https://pkg.go.dev/encoding/json#Marshal

我怀疑解决方案是编写自己的JSON编码器以生成你想要的JSON。

示例中包含了一个自定义编码器的文档:

https://pkg.go.dev/encoding/json#example-package-CustomMarshalJSON

这里是一个示例:https://play.golang.org/p/N2K8QfmPNnc

由于是在手机上费力地输入,所以可能有些不完美,还请谅解。

英文:

In Golang []byte is an alias for []uint8 and json.Marshall encodes []byte as base64-encoded strings (as you're seeing):

https://pkg.go.dev/encoding/json#Marshal

I suspect the solution is to write your own JSON Marshaller to generate JSON as you want.

An example of a custom Marshaller is included in the docs for the package:

https://pkg.go.dev/encoding/json#example-package-CustomMarshalJSON

Here: https://play.golang.org/p/N2K8QfmPNnc

Typed tediously on my phone, so apologies it's hacky

huangapple
  • 本文由 发表于 2021年10月11日 02:47:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/69518024.html
匿名

发表评论

匿名网友

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

确定