duancan5327 2019-02-17 13:15
浏览 67
已采纳

从Go中的另一个嵌套结构变量访问结构的变量

Given are 2 structs I wrote.

type DNSPacket struct {
    header      DNSHeader.DNSHeader
    questions   []DNSQuestion.DNSQuestion
    answers     []DNSRecord.DNSRecord
    authorities []DNSRecord.DNSRecord
    resources   []DNSRecord.DNSRecord
}
type DNSHeader struct {
    id uint16 //16bits

    recursion_desired    bool  // 1 bit
    truncated_message    bool  // 1 bit
    authoritative_answer bool  // 1 bit
    opcode               uint8 // 4 bits
    response             bool  // 1 bit

    rescode             ResultCode // 4 bits
    checking_disabled   bool       // 1 bit
    authed_data         bool       // 1 bit
    z                   bool       // 1 bit
    recursion_available bool       // 1 bit

    questions             uint16 // 16 bits
    answers               uint16 // 16 bits
    authoritative_entries uint16 // 16 bits
    resource_entries      uint16 // 16 bits
} // total 96 bits

Now, when I call results.header.questions where results is a type DNSPacket variable I get a [go] result.header.questions undefined (cannot refer to unexported field or method questions) error.

What am I doing wrong? Is there a workaround?

Thanks.

  • 写回答

1条回答 默认 最新

  • dpd3447 2019-02-17 14:33
    关注

    There are some issues here:

    1. The fields of both your structs are not exported outside the package that contains them, because they begin with a lowercase letter. If you want to access fields outside of the package, they have to start with an uppercase letter.
    2. Name your packages in lowercase, like dnsheader, not DNSHeader.

    Read the spec on exported names and Effective Go on package names

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

报告相同问题?

悬赏问题

  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥60 许可证msc licensing软件报错显示已有相同版本软件,但是下一步显示无法读取日志目录。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加