英文:
GoLang parsing string to struct
问题
如何将字符串转换为结构体?
字符串的格式如下:Name[data1] Name2[data1 data2 data3] Name3[data1 data2] ...
数据可以是Int、String或Float类型。一般来说,这不重要,你可以将所有数据都写成字符串。
方括号中的数据由空格分隔。
使用正则表达式得到的结果如下:
func Parse() {
str := "Version[2f0] Terminal[10002] Machine[10.1.1.1] DH[0.137%] Temp[45] Fan[0] Vo[4074 4042 4058 4098] CRC[0 0 0 0]"
j := make(map[string][]string)
re, err := regexp.Compile(`(?P<Name>[^ \[]*)\[(?P<Value>[^\]]*)`)
if err != nil {
log.Fatal(err)
}
res := re.FindAllSubmatch([]byte(str), -1)
for _, match := range res {
j[string(match[1])] = strings.Split(string(match[2]), " ")
}
log.Printf("%+v", j)
}
是否有更优雅的方法使用json.Unmarshal?
英文:
How do I convert a string to a structure?
The line looks like this: Name[data1] Name2[data1 data2 data3] Name3[data1 data2] ...
The data can be of type Int, String, or Float. In general, this is not important, you can write everything as string.
Data in brackets is separated by a space
What I got using regexp:
func Parse() {
str := "Version[2f0] Terminal[10002] Machine[10.1.1.1] DH[0.137%] Temp[45] Fan[0] Vo[4074 4042 4058 4098] CRC[0 0 0 0]"
j := make(map[string][]string)
re, err := regexp.Compile(`(?P<Name>[^ \[]*)\[(?P<Value>[^\]]*)`)
if err != nil {
log.Fatal(err)
}
res := re.FindAllSubmatch([]byte(str), -1)
for _, match := range res {
j[string(match[1])] = strings.Split(string(match[2]), " ")
}
log.Printf("%+v", j)
}
Could there be a more elegant way using json.Unmarshal?
答案1
得分: 1
如果你可以使用任何你喜欢的工具,正则表达式可能会使这个问题变得相当容易。
(?P<Name>[^\[]*)\[(?P<Values>[^\]]*)\]
然后你可以使用 string.Split 将 Values 组中的空格分隔开。
要创建一个动态结构体,你需要使用反射。不过,在学习Go语言时要避免使用反射。Go语言中的 struct 不同于JavaScript或Python中的对象,它的字段在运行时是静态的。Go语言的 struct 用于定义静态字段。
对于这个问题,map[string][]string 可能是更合适的类型。键将是 Name 字段,值将是 data 字段。
英文:
If you're free to use any tool you like, a regular expression might make this fairly easy.
(?P<Name>\[^\[\]*)\[(?P<Values>\[^\]\]*)\]
That matches each of the named value sets.
and then you could split the Values group at the spaces with string.Split
To create a dynamic struct you'd need to use reflection. Avoid reflection, though, as you learn Go. A struct in Go is unlike an object in say Javascript or Python, where fields are dynamic at runtime. Go structs are meant to have static fields.
For this purpose, a map[string][]string would be a more appropriate type. The key would be the Name fields, and the value would be the data fields.
答案2
得分: 1
你需要为序列化数据定义一个特定的格式。其中一个关键部分是如何编码特定的特殊字符,比如 [ 和 ]。通常情况下,你不希望这样做。
如果你想将任何字符串作为临时文本表示你的数据,并且格式不重要,你可以使用JSON,并使用json.Marshal()和json.Unmarshal()。
我不确定你想要实现什么(这个结构体数据是什么?是什么生成了它?为什么你要将其解码为结构体?),但是了解一些基础知识可能会有帮助:
- https://en.wikipedia.org/wiki/Serialization 将告诉你什么是序列化。
- https://betterprogramming.pub/serialization-and-deserialization-ba12fc3fbe23 是一个非常简短的指南,将为你提供基础知识(使用JavaScript)。
- 我已经提到了
json.Marshal和json.Unmarshal- 阅读它们在标准库中的文档也会有帮助。
当你确定了你需要了解的内容时,我相信你可以通过谷歌搜索更深入的文章。
英文:
- You need to define a specific format for the serializing the data. A key part of that will be how to encode specific special characters, such as the
[and]. You generally do not want to do this. - If you want to use any string as a temporary textual representation of your data and the format doesn't matter, you can just use JSON and use
json.Marshal()andjson.Unmarshal().
I'm not sure what you are trying to achieve (what's this struct data? what generated it? why are you trying to decode it into a struct?) but it seems that going through the fundamentals might be helpful:
- https://en.wikipedia.org/wiki/Serialization will tell you what that is
- https://betterprogramming.pub/serialization-and-deserialization-ba12fc3fbe23 is a very short guide that will give you the very basics (in JavaScript)
- I already mentioned
json.Marshalandjson.Unmarshal- reading their docs in the standard library will be helpful as well.
I'm sure you can google the more in-depth articles when you identify what you need to know for your specific purposes.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论