douliao7930 2014-12-23 19:27
浏览 193
已采纳

字段:值和值初始值设定项的混合

Why can I not create the following, with an anonymous field?

type T1 struct {
    T1_Text string
}

type T2 struct {
    T2_Text string
    T1
}

used in func ..

t := T2{
    T2_Text: "Test",
    T1{T1_Text: "Test"},
}

Gives me: mixture of field:value and value initializers?

  • 写回答

2条回答 默认 最新

  • duanpanhuo0618 2017-11-19 16:53
    关注

    A brief explanation.

    The reason you get that is because you are allowed to only use one of the two types of initializers and not both.

    i.e. you can either use field:value or value.

    Using your example you either do

    field:value

    t := T2{
        T2_Text: "Test",
        T1: T1{T1_Text: "Test"},
    }
    

    or only values

    t := T2{
        "Test",
        T1{"Test"},
    }
    

    Hope that explains the why

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?