动态结构作为参数 Golang

huangapple go评论139阅读模式
英文:

Dynamic struct as parameter Golang

问题

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

type ReturnValue struct {
   Status string
   CustomStruct // 这里应该存储任何给定的结构体
} 

func GetReturn(status string, class interface{}){
   var result = ReturnValue {Status : status, CustomStruct : // 这里应该存储任何结构体}

   fmt.Prinln(result)
}

type Message1 struct {
   message : string
}

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

以下是翻译的代码:

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

    type ReturnValue struct {
       Status string
       CustomStruct // 这里应该存储任何给定的结构体
    } 
    
    func GetReturn(status string, class interface{}){
       var result = ReturnValue {Status : status, CustomStruct : // 这里应该存储任何结构体}
    
       fmt.Prinln(result)
    }
    
    type Message1 struct {
       message : string
    }
    
    func main(){
       var message = Message1 {message: "Hello"}
       GetReturn("success",  message)
    }

希望这对你有帮助!

英文:

I'm wondering whether it is possible to pass a dynamic struct as function's parameter ?

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)
}

答案1

得分: 11

你可以使用这样的接口和if语句将其转换回原始的结构体。

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)
}

链接:https://play.golang.org/p/L6VYV80x27

英文:

You could use an interface like this and an if statement to get it back to whatever struct it orginated as 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)
}

https://play.golang.org/p/L6VYV80x27

huangapple
  • 本文由 发表于 2017年7月17日 17:02:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/45139954.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定