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.

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

报告相同问题?

悬赏问题

  • ¥15 slaris 系统断电后,重新开机后一直自动重启
  • ¥15 51寻迹小车定点寻迹
  • ¥15 谁能帮我看看这拒稿理由啥意思啊阿啊
  • ¥15 关于vue2中methods使用call修改this指向的问题
  • ¥15 idea自动补全键位冲突
  • ¥15 请教一下写代码,代码好难
  • ¥15 iis10中如何阻止别人网站重定向到我的网站
  • ¥15 滑块验证码移动速度不一致问题
  • ¥15 Utunbu中vscode下cern root工作台中写的程序root的头文件无法包含
  • ¥15 麒麟V10桌面版SP1如何配置bonding