gpt4 book ai didi

go - 返回值指针语法

转载 作者:IT王子 更新时间:2023-10-29 02:29:19 26 4
gpt4 key购买 nike

在下面的代码中:

type boolValue bool

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

最后一行是做什么的?

最佳答案

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) 正在执行从 *bool(p 的类型)到 * 的类型转换boolValuenewBool​​Value函数返回值的类型。 Go 需要显式转换。允许转换,因为 boolboolValue 的基础类型。

如果您只是简单地编写 return p 而不进行转换,编译器错误消息会解释问题:

return p

error: cannot use p (type *bool) as type *boolValue in return argument

关于go - 返回值指针语法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43999733/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com