douyong7199 2017-06-30 13:49 采纳率: 0%
浏览 8
已采纳

确定go函数返回的内容

Given this function:

func (c *Firehose) PutRecord(input *PutRecordInput) (*PutRecordOutput, error) {
    req, out := c.PutRecordRequest(input)
    return out, req.Send()
}

I found that this invocation works:

err, _ := svc.PutRecord(putRecordInput)

However I'm still not very clear on what this means in function signature:

(*PutRecordOutput, error)

My question is, can I always determine what is returned from a function by what is specified in the return line, which in this case is:

return out, req.Send()

  • 写回答

1条回答 默认 最新

  • dongxian8858 2017-06-30 13:55
    关注

    This part of the function signature is exactly what the function returns.

    (*PutRecordOutput, error)
    

    So this one will return a pointer to a PutRecordOutput plus an error (which by convention is returned as nil if no error occurred).

    If you look at the source code for the function, return statements will have to be consistent with that, so that can also help you understand how the return values are built.

    But, please note that in some cases you could have named output arguments, like:

    (output *PutRecordOutput, err error)
    

    In that case, output and err will be valid local variables inside the function, and you might see a plain return statement like this:

    return
    

    Just keep in mind that one would implicitly be like doing:

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

报告相同问题?

悬赏问题

  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?