douzen1880 2019-04-10 08:54
浏览 39
已采纳

非导出类型的类型声明

I am using a 3rd party package, which allows you to create structures of a certain non-exported type through an exported function.

package squirrel

type expr struct {
    sql string
    args []interface{}
}

func Expr(sql string, args ...interface{}) expr {
    return expr{sql: sql, args: args}
}

Because of the way some other function of this library accepts data, I ended up with such a map:

m := map[string]interface{} {
    "col1": 123,
    "col2": "a_string",
    "col3": Expr("now()"),
}

but because of a different function in this library I need to filter out all squirrel.expr from this map.

Obviously, I wasn't able to assert the type directly, by doing so:

filtered := make(map[string]interface{})
for k, v := range m {
    switch v.(type) {
    case squirrel.expr:
        continue
    default:
        filtered[k] = v
    }
}

Is there another way to achieve the same result?

  • 写回答

1条回答 默认 最新

  • dshdb64088 2019-04-10 09:02
    关注

    You may use reflection to compare the type of values to the type of squirrel.expr. Type here means the reflect.Type descriptors, acquired by reflect.TypeOf().

    For example:

    m := map[string]interface{}{
        "col1": 123,
        "col2": "a_string",
        "col3": squirrel.Expr("now()"),
    }
    fmt.Println(m)
    
    exprType := reflect.TypeOf(squirrel.Expr(""))
    
    filtered := make(map[string]interface{})
    for k, v := range m {
        if reflect.TypeOf(v) == exprType {
            continue
        }
        filtered[k] = v
    }
    fmt.Println(filtered)
    

    This will output:

    map[col1:123 col2:a_string col3:{now() []}]
    map[col1:123 col2:a_string]
    

    Note:

    We obtained the reflect.Type descriptor of the values we want to filter out by passing the return value of a squirrel.Expr() call (which is of type squirrel.expr). This is fine in this case, but if it is unfeasible to call this function just to get the type (e.g. the call has side effects which must be avoided), we can avoid that. We can use reflection to obtain the reflect.Type descriptor of the squirrel.Expr function itself, and get the type descriptor of its return type. This is how it could be done:

    exprType := reflect.TypeOf(squirrel.Expr).Out(0)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 连续两帧图像高速减法
  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写