doulao2128 2017-05-16 11:05
浏览 8
已采纳

返回值指针语法

In the following code:

type boolValue bool

func newBoolValue(val bool, p *bool) *boolValue {
    *p = val
    return (*boolValue)(p)
}

What is the last line doing?

  • 写回答

3条回答 默认 最新

  • dongzhao4036 2017-05-16 11:12
    关注

    The Go Programming Language Specification

    Conversions

    Conversions are expressions of the form T(x) where T is a type and x is an expression that can be converted to type T.

    type boolValue bool
    
    func newBoolValue(val bool, p *bool) *boolValue {
        *p = val
        return (*boolValue)(p)
    }
    

    (*boolValue)(p) is doing a type conversion from *bool, the type of p, to *boolValue, the type of the newBoolValue function return value. Go requires explicit conversions. The conversion is allowed because bool is the underlying type of boolValue.

    If you simply write return p without the conversion the compiler error message explains the problem:

    return p
    
    error: cannot use p (type *bool) as type *boolValue in return argument
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥30 关于#java#的问题,请各位专家解答!
  • ¥30 vue+element根据数据循环生成多个table,如何实现最后一列 平均分 合并
  • ¥20 pcf8563时钟芯片不启振
  • ¥20 pip2.40更新pip2.43时报错
  • ¥15 换yum源但仍然用不了httpd
  • ¥50 C# 使用DEVMOD设置打印机首选项
  • ¥15 麒麟V10 arm安装gdal
  • ¥20 OPENVPN连接问题
  • ¥15 flask实现搜索框访问数据库
  • ¥15 mrk3399刷完安卓11后投屏调试只能显示一个设备