dongwen7371 2017-06-28 14:53
浏览 67
已采纳

Golang接口到结构

I have a function that has a parameter with the type interface{}, something like:

func LoadTemplate(templateData interface{}) {

In my case, templateData is a struct, but each time it has a different structure. I used the type "interface{}" because it allows me to send all kind of data.

I'm using this templateData to send the data to the template:

err := tmpl.ExecuteTemplate(w, baseTemplateName, templateData)

But now I want to append some new data and I don't know how to do it because the "interface" type doesn't allow me to add/append anything.

I tried to convert the interface to a struct, but I don't know how to append data to a struct with an unknown structure.

If I use the following function I can see the interface's data:

templateData = appendAssetsToTemplateData(templateData)

func appendAssetsToTemplateData(t interface{}) interface{} {
    switch reflect.TypeOf(t).Kind() {
    case reflect.Struct:
        fmt.Println("struct")
        s := reflect.ValueOf(t)
        fmt.Println(s)

        //create a new struct based on current interface data
    }

    return t
}

Any idea how can I append a child to the initial interface parameter (templateData)? Or how can I transform it to a struct or something else in order to append the new child/data?

  • 写回答

4条回答 默认 最新

  • doutong6814 2017-06-28 22:22
    关注

    Adrian is correct. To take it a step further, you can only do anything with interfaces if you know the type that implements that interface. The empty interface, interface{} isn't really an "anything" value like is commonly misunderstood; it is just an interface that is immediately satisfied by all types.

    Therefore, you can only get values from it or create a new "interface" with added values by knowing the type satisfying the empty interface before and after the addition.

    The closest you can come to doing what you want, given the static typing, is by embedding the before type in the after type, so that everything can still be accessed at the root of the after type. The following illustrates this.

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

    package main
    
    import (
        "fmt"
    )
    
    type Before struct {
        m map[string]string
    }
    
    type After struct {
        Before
        s []string
    }
    
    func contrivedAfter(b interface{}) interface{} {
        return After{b.(Before), []string{"new value"}}
    }
    
    func main() {
        b := Before{map[string]string{"some": "value"}}
        a := contrivedAfter(b).(After)
        fmt.Println(a.m)
        fmt.Println(a.s)
    }
    

    Additionally, since the data you are passing to the template does not require you to specify the type, you could use an anonymous struct to accomplish something very similar.

    https://play.golang.org/p/3KUfHULR84

    package main
    
    import (
        "fmt"
    )
    
    type Before struct {
        m map[string]string
    }
    
    func contrivedAfter(b interface{}) interface{} {
        return struct{
            Before
            s []string
        }{b.(Before), []string{"new value"}}
    }
    
    func main() {
        b := Before{map[string]string{"some": "value"}}
        a := contrivedAfter(b)
        fmt.Println(a)
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥15 python天天向上类似问题,但没有清零
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 C#调用python代码(python带有库)
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?