Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
动态结构作为参数Golang_Go - Fatal编程技术网

动态结构作为参数Golang

动态结构作为参数Golang,go,Go,我想知道是否可以将动态结构作为函数的参数传递 type ReturnValue struct { Status string CustomStruct // here should store any struct given } func GetReturn(status string, class interface{}){ var result = ReturnValue {Status : status, CustomStruct : //here any stru

我想知道是否可以将动态结构作为函数的参数传递

type ReturnValue struct {
   Status string
   CustomStruct // here should store any struct given
} 

func GetReturn(status string, class interface{}){
   var result = ReturnValue {Status : status, CustomStruct : //here any struct should be stored}

   fmt.Prinln(result)
}

type Message1 struct {
   message : string
}

func main(){
   var message = Message1 {message: "Hello"}
   GetReturn("success",  message)
}

您可以使用像这样的接口和if语句将其返回到它作为tho组织的任何结构

import (
    "fmt"
)

type ReturnValue struct {
   Status string
   CustomStruct interface{}
} 

func GetReturn(status string, class interface{}){
   var result = ReturnValue {Status : status, CustomStruct: class}

   fmt.Println(result)

   msg, ok := result.CustomStruct.(Message1)
   if ok {
      fmt.Printf("Message1 is %s\n", msg.message)
   }
}

type Message1 struct {
   message string
}

type Message2 struct {
   message string
}

func main(){
   var m1 = Message1 {message: "Hello1"}
   GetReturn("success",  m1)

   var m2 = Message2 {message: "Hello2"}
   GetReturn("success",  m2)
}