英文:
Unmarshaling string to struct like structure in Golang
问题
我得到了以下的Golang字符串:
var cars = [
{
model: "SLR",
brand: "Mercedes",
prices: [520, 730],
},
{
model: "M4",
brand: "BMW",
prices: [420, 820],
}
]
我知道这不是JSON字符串。
有没有办法“解组”这个字符串并获取每辆车的型号?
我该如何获取每辆车的型号?
或者这本来就是一个错误的问题?我很想听听你的意见。
英文:
I was given a string below with Golang:
var cars = [
{
model: "SLR",
brand: "Mercedes",
prices: [520, 730],
},
{
model: "M4",
brand: "BMW",
prices: [420, 820],
}
]
I know it is not JSON string.
Is there any way at all to "unmarshal" the string and get the models of each car?
How can I get the model of each car?
or is it a wrong question in the first place? I appreciate your opinion.
答案1
得分: 4
为了解析您的输入并获取每辆汽车的型号,您需要使用unmarshal
方法和range
方法。我已经按照您的情况进行了重现,代码如下:
package main
import (
"fmt"
"gopkg.in/yaml.v2"
)
type Car struct {
Model string `yaml:"model"`
Brand string `yaml:"brand"`
Prices []int `yaml:"prices"`
}
func main() {
var car = []byte(`[ { model: "SLR", brand: "Mercedes", prices: [520, 730], }, { model: "M4", brand: "BMW", prices: [420, 820], } ]`)
var cars []Car
if err := yaml.Unmarshal(car, &cars); err != nil {
panic(err)
}
for _, v := range cars {
fmt.Println(v.Model)
}
}
输出结果:
SLR
M4
英文:
To unmarshal your input and retrieve the model of each car you have to use umarshal method followed by range
method,I have reproduced your scenario as follows :
package main
import (
"fmt"
"gopkg.in/yaml.v2"
)
type Car struct {
Model string `yaml:"model"`
Brand string `yaml:"brand"`
Prices []int `yaml:"prices"`
}
func main() {
var car = []byte(`[ { model: "SLR", brand: "Mercedes", prices: [520, 730], }, { model: "M4", brand: "BMW", prices: [420, 820], } ]`)
var cars []Car
if err := yaml.Unmarshal(car, &cars); err != nil {
panic(err)
}
for _, v := range cars {
fmt.Println(v.Model)
}
}
Output:
SLR
M4
答案2
得分: 1
你可以尝试使用yaml来处理你的输入:
package main
import (
"fmt"
"gopkg.in/yaml.v2"
)
func main() {
const cars = `[ { model: "SLR", brand: "Mercedes", prices: [520, 730], }, { model: "M4", brand: "BMW", prices: [420, 820], } ]`
type Car struct {
Model string `yaml:"model"`
Brand string `yaml:"brand"`
Prices []int `yaml:"prices"`
}
var carsArr []Car
if err := yaml.Unmarshal([]byte(cars), &carsArr); err != nil {
panic(err)
}
fmt.Printf("Cars: %+v", carsArr)
// Cars: [{Model:SLR Brand:Mercedes Prices:[520 730]} {Model:M4 Brand:BMW Prices:[420 820]}]
}
在这里尝试:https://play.golang.org/p/hRQXSes1tGi
英文:
You may have luck using yaml for your input:
package main
import (
"fmt"
"gopkg.in/yaml.v2"
)
func main() {
const cars = `[ { model: "SLR", brand: "Mercedes", prices: [520, 730], }, { model: "M4", brand: "BMW", prices: [420, 820], } ]`
type Car struct {
Model string `yaml:"model"`
Brand string `yaml:"brand"`
Prices []int `yaml:"prices"`
}
var carsArr []Car
if err := yaml.Unmarshal([]byte(cars), &carsArr); err != nil {
panic(err)
}
fmt.Printf("Cars: %+v", carsArr)
// Cars: [{Model:SLR Brand:Mercedes Prices:[520 730]} {Model:M4 Brand:BMW Prices:[420 820]}]
}
Try it here: https://play.golang.org/p/hRQXSes1tGi
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论