Go 如何将任何结构作为参数发送给方法并返回结构?

Go 如何将任何结构作为参数发送给方法并返回结构?,go,struct,yaml,Go,Struct,Yaml,我是新手,我想知道是否有一种方法可以接收任何结构作为参数 我的代码中有这样一个函数,它对5个结构执行完全相同的操作,并返回相同的结构,但我不知道我是否可以这样做。我想知道我是否可以这样做: type Car struct { Model string `yaml:"Model"` Color string `yaml:"Color"` Wheels int `yaml:Wheels"` Windows int `yaml:"Windo

我是新手,我想知道是否有一种方法可以接收任何结构作为参数

我的代码中有这样一个函数,它对5个结构执行完全相同的操作,并返回相同的结构,但我不知道我是否可以这样做。我想知道我是否可以这样做:

type Car struct {
     Model   string `yaml:"Model"`
     Color   string `yaml:"Color"`
     Wheels  int    `yaml:Wheels"`
     Windows int    `yaml:"Windows"` 
}

type Motorcycle struct {
     Model    string `yaml:"Model"`
     Color    string `yaml:"Color"`
     Wheels   int      `yaml:Wheels"`
}

type Bus struct {
     Model      string `yaml:"Model"`
     Color      string `yaml:"Color"`
     Wheels     int    `yaml:Wheels"`
     Passengers int    `yaml:"Passengers"`
}

func main () {

    car := GetYamlData(Car)
    motorcycle := GetYamlData(Motorcycle)
    Bus := GetYamlData(Bus)
}

func GetYamlData(struct anyStructure) (ReturnAnyStruct struct){

       yaml.Unmarshal(yamlFile, &anyStructure)

       return anyStructure
}
func main(){
   car, _, _ := GetYamlData("car")
    _,motorcycle,_ := GetYamlData("motorcycle")
    _,_,bus := GetYamlData("bus")
 }

func GetYamlData(structureType string) (car *Car, motorcycle *Motorcycle, bus *Bus){

 switch structureType{

        case "car":
                yaml.Unmarshal(Filepath, car)
        case "motorcycle":
                yaml.Unmarshal(Filepath, motorcycle)
        case "bus":
                yaml.Unmarshal(Filepath, bus)
 }

 return car, motorcycle, bus

}
是否可以执行类似上述代码的操作?事实上,我得到的是这样的东西:

type Car struct {
     Model   string `yaml:"Model"`
     Color   string `yaml:"Color"`
     Wheels  int    `yaml:Wheels"`
     Windows int    `yaml:"Windows"` 
}

type Motorcycle struct {
     Model    string `yaml:"Model"`
     Color    string `yaml:"Color"`
     Wheels   int      `yaml:Wheels"`
}

type Bus struct {
     Model      string `yaml:"Model"`
     Color      string `yaml:"Color"`
     Wheels     int    `yaml:Wheels"`
     Passengers int    `yaml:"Passengers"`
}

func main () {

    car := GetYamlData(Car)
    motorcycle := GetYamlData(Motorcycle)
    Bus := GetYamlData(Bus)
}

func GetYamlData(struct anyStructure) (ReturnAnyStruct struct){

       yaml.Unmarshal(yamlFile, &anyStructure)

       return anyStructure
}
func main(){
   car, _, _ := GetYamlData("car")
    _,motorcycle,_ := GetYamlData("motorcycle")
    _,_,bus := GetYamlData("bus")
 }

func GetYamlData(structureType string) (car *Car, motorcycle *Motorcycle, bus *Bus){

 switch structureType{

        case "car":
                yaml.Unmarshal(Filepath, car)
        case "motorcycle":
                yaml.Unmarshal(Filepath, motorcycle)
        case "bus":
                yaml.Unmarshal(Filepath, bus)
 }

 return car, motorcycle, bus

}

随着时间的推移,这将增加,它将返回很多值,我不想返回很多值,有没有办法用我发布的第一个代码执行此操作?

您可以使用与
yaml完全相同的方法来执行此操作。
Unmarshal
通过将要解组的值带入:

func GetYamlData(i interface{}) {
    yaml.Unmarshal(Filepath, i)    
}
用法示例:

func main () {
    var car Car
    var motorcycle Motorcycle
    var bus Bus
    GetYamlData(&car)
    GetYamlData(&motorcycle)
    GetYamlData(&bus)
}

您可以完全按照
yaml.Unmarshal
的方法执行,方法是将要解组的值带入:

func GetYamlData(i interface{}) {
    yaml.Unmarshal(Filepath, i)    
}
用法示例:

func main () {
    var car Car
    var motorcycle Motorcycle
    var bus Bus
    GetYamlData(&car)
    GetYamlData(&motorcycle)
    GetYamlData(&bus)
}

让具有相同结构的类型以完全相同的方式处理的目的是什么?好吧,这只是一个例子,我不想发布我的真实代码,但这些结构是不同的值,它们是可变的,它们不一样,我将编辑我的帖子以避免任何混淆。让具有相同结构的类型以完全相同的方式处理的目的是什么?好吧,这只是一个例子,我不想发布我的真实代码,但这些结构是不同的值,它们是可变的,它们不一样,我将编辑我的帖子以避免任何混淆。如何返回填充了值的结构并将其分配给结构,我的意思是:
car:=GetYamlData(car)
返回填充了值的.yaml并将其分配给新的结构对象您可以获得与上面建议的解决方案完全相同的行为。有什么特别的理由坚持它是一个返回值而不是一个引用参数吗?因为这将大大增加解决方案的复杂性。感谢Adrian,上面的代码正在工作,但结构的值没有填充,而是在界面中查找,它正在检索数据,我只需要弄清楚如何将它们分配给结构的相应值。我不确定我是否理解-你说它正在填充值,但它没有填充值?是哪个?我的意思是用函数“GetYamlData”填充“struct”属性,接口包含.yaml文件中的数据,但我必须弄清楚如何将.yaml值分配给结构属性。如何返回填充了值的结构并将其分配给结构,我的意思是:
car:=GetYamlData(car)
返回填充了它们的值的.yaml,并将其分配给一个新的结构对象。您可以获得与上面建议的解决方案完全相同的行为。有什么特别的理由坚持它是一个返回值而不是一个引用参数吗?因为这将大大增加解决方案的复杂性。感谢Adrian,上面的代码正在工作,但结构的值没有填充,而是在界面中查找,它正在检索数据,我只需要弄清楚如何将它们分配给结构的相应值。我不确定我是否理解-你说它正在填充值,但它没有填充值?这是哪一个?我的意思是用函数“GetYamlData”填充“struct”属性,接口包含来自.yaml文件的数据,但我必须弄清楚如何将.yaml值分配给结构属性。