douqipi9704 2014-11-21 13:09
浏览 73
已采纳

GO Golang:匿名结构和反射组合

After reading the laws of reflection about 10 times the last 2 months. Developing with it for the same amount of time, i have to say it is a cool and understandable language... at least to an certain level.

My background as PHP & Javascript developer are giving me an hard time to comprehend the following example:

package main

import(
    "fmt"
    "reflect"
)


func test1(){
    type template struct {
        Title string
        Body  string
    }

    data := []template{
        { Title : "About page", Body : "Body info"},
        { Body : "About page 2 ", Title : "Body info 2"},
    }

    fmt.Println( "-- TEST ONE --" ) 
    fmt.Println( data[0].Title )
}

func test2(){
    data := []struct{
        Title string
        Body  string
    }{
        // Assign with the actual order
        { "About page", "Body info"},
        // Key => Val assignment (Pretty cool)
        { Body : "Body info 2 ", Title : "About page 2"},
    }

    fmt.Println( "-- TEST TWO --" ) 
    fmt.Println( data[1].Title )
}

func test3(){
    type template struct {
        Title string
        Body  string
    }

    Amap := map[string]interface{}{
        "template" : template{},
    }

    w := reflect.ValueOf(Amap["template"])
    x := w.Type()
    y := reflect.TypeOf(w.Interface())
    z := reflect.TypeOf(Amap["template"])

    fmt.Printf("%+v
", x) // main.template
    fmt.Printf("%+v
", y) // main.template
    fmt.Printf("%+v
", z) // main.template

    /*
    var data = struct{
        // none of the above can be place in here.... ( (w|x|y|z) is not a type)
    }{ "About page", "Body info"}
    */

    ww := reflect.New(z)
    xx := ww.Interface()
    tt := reflect.TypeOf(xx)

    /*
    // none of the above can be used this way....
    var data = ww{

    }{ "About page", "Body info"}
    */

    fmt.Println( "-- TEST THREE --" ) 
    fmt.Println( data.Title )
}

func main(){
    test1()
    test2()
    test3()
}

The above example test1() and test2() work as expected. I wanted to push it even further with test3() but without success. The only way i could think of making it work was with an type switch..

But since I am trying things out I'd like to know if :

  1. Is there an way to cast an anonymous struct from an reflection value without type checking the actual struct that is being reflected
  2. Could you show me an working solution to either one of the 2 commented out code blocks from test3()
  • 写回答

1条回答 默认 最新

  • duanhuantong8278 2014-11-21 15:12
    关注
    1. Is there a way to cast 1 an anonymous struct from an reflection value without type checking [2] the actual struct that is being reflected?
    1. There is no casting in Go. If you mean a conversion then you're missing a predicate: convert to what?
    2. Type checking is something a compiler does to a program. This sentence does not make sense.
    1. Could you show me a working solution to either one of the 2 commented out code blocks from test3()

    That's simple, just write:

    var data = struct{string, string}{"About page", "Body info"}
    

    If you mean to build/create/assemble a struct type on runtime I'll have to disappoint you; that's not possible.

    Edit (11. Feb 2015): building struct types (as well as arrays, funcs and interfaces) on runtime by means of reflection is being implemented.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c