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 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化