多返回值的常量声明

huangapple go评论88阅读模式
英文:

Const declaration with multiple return values

问题

我想要定义一个常量值:

 const var *url.URL = url.Parse("http://yahoo.com/")

我知道我必须完全定义变量及其类型。也就是说,我不能只使用":="这个简写形式。

但是,从评估函数返回的值既包括常量又包括错误。

 var, _ := url.Parse("http://yahoo.com/")

那么,在这种情况下,我如何声明var是一个常量并且丢弃错误呢?

英文:

I am wanting to define a constant value:

 const var *url.URL = url.Parse("http://yahoo.com/")

I know I have to fully define the variable and its type. ie I cant just use the ":=" shorthand.

But the return value from the evaluated function returns both the constant and a error.

  var, _ := url.Parse("http://yahoo.com/")

Now how do I declare that var is constant and discard the error in this case?

答案1

得分: 8

首先,你不需要指定类型,只需简单地写var foo = <expression>即可。
你需要使用var而不是:=的唯一原因是短变量声明只允许在函数内部使用,而你是在函数外部操作。

其次,你不能将函数调用用于常量值,因为这些值不是常量(函数必须被计算,这与Go语言对常量的定义相违背)。参见常量的规范

常量值由符文、整数、浮点数、虚数、字符串字面量、表示常量的标识符、常量表达式、结果为常量的转换,或应用于任何值的一些内置函数(如应用于任何值的unsafe.Sizeof、应用于某些表达式的cap或len、应用于复数常量的real和imag,以及应用于数值常量的complex)。布尔真值由预声明的常量true和false表示。预声明的标识符iota表示一个整数常量。

这里没有用户定义的函数。

你可以定义一个var(在playground上的示例2):

func MustParse(s string) url.URL {
    url, err := url.Parse(s)
    if err != nil { 
        panic(err); 
    }
    return *url
}

var foo = MustParse("http://yahoo.com/")

当然,你也可以这样做:

var foo, _ = url.Parse("foo")

但是这样你就无法知道你的URL是否正确。

英文:

First of all, you don't need to specify the type, you can simply write var foo = &lt;expression&gt;.
The only reason why you need to use var instead of := is that short variable declarations are only allowed in functions but you're operating outside of functions.

Secondly, you can't use function calls for constant values as these would not be constant (the function must be evaluated, that's against Go's definition of constant). See also the spec on what constants are:

> A constant value is represented by a rune, integer, floating-point, imaginary, or string literal, an identifier denoting a constant, a constant expression, a conversion with a result that is a constant, or the result value of some built-in functions such as unsafe.Sizeof applied to any value, cap or len applied to some expressions, real and imag applied to a complex constant and complex applied to numeric constants. The boolean truth values are represented by the predeclared constants true and false. The predeclared identifier iota denotes an integer constant.

No user-defined functions here.

What you can do is to define a var (on play):

func MustParse(s string) url.URL {
	url, err := url.Parse(s)
	if err != nil { 
		panic(err); 
	}
	return *url
}

var foo = MustParse(&quot;http://yahoo.com/&quot;)

Of course you could also do

var foo, _ = url.Parse(&quot;foo&quot;)

but with this you wouldn't see if your URL is wrong.

答案2

得分: 3

你不能做这两个。抱歉。首先:Go语言中没有常量变量。其次:你不能将变量命名为var,因为var是一个关键字。第三:在函数体外部不允许使用短变量声明。

英文:

You cannot do neither. Sorry. First: There are no const variables in Go. Second: You cannot name a variable var as var is a keyword. Third: No short variable declaration are allowed outside a function body.

huangapple
  • 本文由 发表于 2014年2月20日 02:05:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/21888721.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定