如何在golang中将数据结构作为参数传递 [英] How to pass data struct as parameter in golang

查看:46
本文介绍了如何在golang中将数据结构作为参数传递的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将XML转换为Json

XML to Json

package main

import (
    "encoding/json"
    "encoding/xml"
    "fmt"
)

type Persons struct {
    Person []struct {
        Name string
        Age  int
    }
}
type Places struct {
    Place []struct {
        Name    string
        Country string
    }
}

type Parks struct {
    Park struct {
        Name     []string
        Capacity []int
    }
}

const personXml = `
    <Persons>
        <Person><Name>Koti</Name><Age>30</Age></Person>
        <Person><Name>Kanna</Name><Age>29</Age></Person>
    </Persons>
`

const placeXml = `
    <Places>
        <Place><Name>Chennai</Name><Country>India</Country></Place>
        <Place><Name>London</Name><Country>UK</Country></Place>
    </Places>
`

const parkXml = `
    <Parks>
        <Park><Name>National Park</Name><Capacity>10000</Capacity></Park>
        <Park>Asian Park</Name><Capacity>20000</Capacity></Park>
    </Parks>
`

func WhatIamUsing() {
    var persons Persons
    xml.Unmarshal([]byte(personXml), &persons)
    per, _ := json.Marshal(persons)
    fmt.Printf("%s\n", per)

    var places Places
    xml.Unmarshal([]byte(placeXml), &places)
    pla, _ := json.Marshal(places)
    fmt.Printf("%s\n", pla)

    var parks Parks
    xml.Unmarshal([]byte(parkXml), &parks)
    par, _ := json.Marshal(parks)
    fmt.Printf("%s\n", par)
}

我想要的是一个通用函数,它需要xml字符串和dataStruct并返回一个Json输出.但是下面的函数抛出错误如何隐含这一点?

What i want is a generic function which takes xml string and dataStruct and returns a Json output. But below function is throwing an error How to impliment this?

func Xml2Json(xmlString string, DataStruct interface{}) (jsobj string, err error) {
    var dataStruct DataStruct
    xml.Unmarshal([]byte(personXml), &dataStruct)
    js, _ := json.Marshal(dataStruct)
    return fmt.Sprintf("%s\n", js), nil
}

func main() {
    jsonstring, _ := Xml2Json(personXml, Persons)
}

错误消息:

prog.go:73:DataStruct不是类型

prog.go:73: DataStruct is not a type

prog.go:80:类型Persons不是表达式

prog.go:80: type Persons is not an expression

goplay链接: http://play.golang.org/p/vayb0bawKx

goplay link: http://play.golang.org/p/vayb0bawKx

推荐答案

您不能在接口中存储类型(例如 Persons ).您可以将 reflect.Type 传递给函数.然后,您的呼叫将看起来像 Xml2Json(personXml,reflect.TypeOf(Persons)),在我看来,这非常难看.

You can not store a type (like Persons) in an interface. You could pass a reflect.Type to your function. Then, your call would look like Xml2Json(personXml, reflect.TypeOf(Persons)) which is quite ugly in my opinion.

更好的方法可能是:

func Xml2Json(xmlString string, value interface{}) (string, error) {
    if err := xml.Unmarshal([]byte(xmlString), value); err != nil {
        return "", err
    }
    js, err := json.Marshal(value)
    if err != nil {
        return "", err
    }
    return string(js), nil
}

如果您对值本身不感兴趣,可以将此函数与 Xml2Json(personXml,new(Persons))一起使用,并且

You can use this function with Xml2Json(personXml, new(Persons)) if you are not interested in the value itself, and

var persons Persons
Xml2Json(personXML, &persons)

当您还希望检索结构值以供以后处理时.

when you also want to retrieve the struct value for later processing.

这篇关于如何在golang中将数据结构作为参数传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆