duanchu9914 2019-05-28 04:10
浏览 28
已采纳

等效结构上字段的接口转换

Is there a straightforward way to convert a struct where some fields are "generic" (interface{}) into another type which has the same field names but is "strongly-typed" (int, string, etc)?

Lets say that, given the definitions:

package main

import (
    "fmt"
)

type GenericData struct {
    Hard int
    Soft interface{}
}

type Data struct {
    Hard int
    Soft int
}

type GenericDataGenerator func() GenericData

func generateGenericData() interface{} {
    return GenericData{1, 2}
}

func returnsGenericDataGenerator() interface{} {
    return generateGenericData
}

Does converting from GenericData to Data require explicitly casting all interface{} fields? Is there a more straightforwad way to do this?

func main() {
    gd := generateGenericData()
    fmt.Println(gd)
    fmt.Println(gd.(GenericData))

    // Doesn't work, but is straightforward
    // fmt.Println(gd.(Data))

    // Works, but is not straight forward
    fmt.Println(Data{gd.(GenericData).Hard, gd.(GenericData).Soft.(int)})


    genDataGenerator := returnsGenericDataGenerator()

    // Doesn't work, but is straightforward
    //genDataGenerator.(GenericDataGenerator)()

    // Works, but is not straight forward
    resp := genDataGenerator.(func() interface{})()
    fmt.Println(resp.(GenericData))
}

The code can be executed in: https://play.golang.org/p/_UVBi5It1FY

  • 写回答

1条回答 默认 最新

  • dongqu5650 2019-05-28 05:54
    关注

    Is there a straightforward way to convert a struct where some fields are "generic" (interface{}) into another type which has the same field names but is "strongly-typed" (int, string, etc)?

    No.

    And this is obvious once you understand that interface{} is not some kind of "generic" or "arbitrary" type but a single, "strongly-type" type just like string or int32. The empty interface interface{} is a totally normal type just like a uint16 or a map[float32]bool is. It is a interface type (in contrast to the other examples which are a numeric type and a map type) and its method set is the empty set. It is as strict or strongly typed as any of the other types.

    The fact that you can assign any value to a variable of type interface{} does not mean that interface{} somehow means "any type". You can assign the values 3, -1234and 435948 (to name a few) to a int32. This is allowed because the allowed value range of a int32 contains these values (3, -1234, 435948). You can assign a value of type func(int) string to a variable of type interface{} because the allowed value range of interface{} contains func(int)string. This is a vacuous truth and not a "generic" one.

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

报告相同问题?

悬赏问题

  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度