doushenmao9036 2015-07-01 08:27
浏览 30
已采纳

在golang中,为什么不能将结构用作嵌套结构类型?

The following code (play):

func main() {
    buf := bytes.NewBuffer(make([]byte, 0))
    rw := bufio.NewReadWriter(bufio.NewReader(buf), bufio.NewWriter(buf))
    var r *bufio.Writer
    r = rw
}

Gives the following compile-time error:

cannot use rw (type *bufio.ReadWriter) as type *bufio.Writer in assignment

What I expected is use a struct as a nested struct type. But if I declare r as io.Reader, this will be ok, so should I move to interface?

  • 写回答

2条回答 默认 最新

  • duannao8450 2015-07-01 08:52
    关注

    bufio.NewReadWriter() returns a concrete type, a pointer to a struct and bufio.Writer is also a concrete type, a struct. Neither *ReadWriter and *bufio.Writer is an interface!

    In Go there is no automatic type conversion, you cannot assign a value of different concrete type to a variable.

    You have 2 options:

    1. Since bufio.ReadWriter embeds *bufio.Writer, you can simply refer to it and use that in the assignment:

      var r *bufio.Writer
      r = rw.Writer
      
    2. Or you can declare r to be an io.Writer (it is an interface type) so that you can assign rw to it because rw implements io.Writer:

      var r io.Writer
      r = rw
      

      Although I don't think creating r in this case is particularly useful because whenever you would use r you could also use rw.

    Check out Go spec: Assignability:

    A value x is assignable to a variable of type T ("x is assignable to T") in any of these cases:

    • x's type is identical to T.
    • x's type V and T have identical underlying types and at least one of V or T is not a named type.
    • T is an interface type and x implements T.
    • x is a bidirectional channel value, T is a channel type, x's type V and T have identical element types, and at least one of V or T is not a named type.
    • x is the predeclared identifier nil and T is a pointer, function, slice, map, channel, or interface type.
    • x is an untyped constant representable by a value of type T.

    None of the cases apply to your code, so it is an invalid assignment.

    When r is declared to be io.Writer, it is the the following case and therefore it is valid:

    T is an interface type and x implements T.

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

报告相同问题?

悬赏问题

  • ¥15 nslt的可用模型,或者其他可以进行推理的现有模型
  • ¥15 arduino上连sim900a实现连接mqtt服务器
  • ¥15 vncviewer7.0安装后如何正确注册License许可证,激活使用
  • ¥15 phython如何实现以下功能?查找同一用户名的消费金额合并2
  • ¥66 关于人体营养与饮食规划的线性规划模型
  • ¥15 基于深度学习的快递面单识别系统
  • ¥15 Multisim仿真设计地铁到站提醒电路
  • ¥15 怎么用一个500W电源给5台60W的电脑供电
  • ¥15 请推荐一个轻量级规则引擎,配合流程引擎使用,规则引擎负责判断出符合规则的流程引擎模板id
  • ¥15 Excel表只有年月怎么计算年龄