在Go编程语言中定义一个变量可以使用以下语法:

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

Defining a variable in Go programming language

问题

我正在学习Go语言,并遇到了这种类型的变量声明:

i:=1;

但是它说Go语言有静态变量,即变量应该以这种方式定义:

var i int=1;

那么这两种方法有什么区别?在第一种方法中,我们不需要指定数据类型。为什么会这样呢?

英文:

I am learning Go language and comes across seeing this type of variable declaration:

i:=1;

But it says that Go has static variables. i,e variables should be defined in some way like this

var i int=1;

So what is the difference between these two methods? In the first one we don't need to indicate the data type. Why is it so?

答案1

得分: 3

第一个 i := 1 被称为短变量声明。它是常规变量声明的简写形式,带有初始化表达式但没有类型:

var IdentifierList = ExpressionList

你没有指定 i 的类型,但是 i 的类型将根据一定的规则进行推断。它的类型将自动推断。在这种情况下,它的类型将是 int,因为初始化表达式 1 是一个无类型的整数常量,其默认类型是 int,所以当需要类型时(例如在短变量声明中使用),将推断出 int 类型。

因此,Go 是静态类型的。这意味着变量将具有静态类型,并且在运行时存储在其中的值始终是该类型。静态类型并不意味着 必须显式指定静态类型,它只是意味着变量必须具有静态类型 - 在编译时决定的类型 - 即使你使用短变量声明并且没有指定类型。

请注意,如果你使用 var 关键字声明变量,也可以省略类型:

var i = 1

在这种情况下,类型也将根据初始化表达式的类型进行推断。

规范:变量声明

> 如果存在类型,则每个变量都被赋予该类型。否则,每个变量都被赋予赋值中相应初始化值的类型。如果该值是无类型常量,则首先将其转换为其默认类型;如果它是无类型布尔值,则首先将其转换为 bool 类型。预声明的值 nil 不能用于初始化没有显式类型的变量。

英文:

The first one i := 1 is called short variable declaration. It is a shorthand for regular variable declaration with initializer expressions but no types:

var IdentifierList = ExpressionList

You don't specify the type of i, but i will have a type based on certain rules. Its type will be automatically inferred. In this case it will be of type int because the initializer expression 1 is an untyped integer constant whose default type is int, so when a type is needed (e.g. it is used in a short variable declaration), int type will be deduced.

So Go is statically typed. That means variables will have a static type and values stored in them at runtime will always be of that type. Being statically typed does not mean you have to explicitly specify the static type, it just means variables must have a static type - decided at compile time - which condition is met even if you use short variable declaration and you don't specify it.

Note that you can also omit the type if you declare a variable with the var keyword:

var i = 1

In which case the type will also be deduced from the type of the initializer expression.

Spec: Variable declaration:

> If a type is present, each variable is given that type. Otherwise, each variable is given the type of the corresponding initialization value in the assignment. If that value is an untyped constant, it is first converted to its default type; if it is an untyped boolean value, it is first converted to type bool. The predeclared value nil cannot be used to initialize a variable with no explicit type.

答案2

得分: 0

Go的设计考虑到了易用性。因此,使用:=运算符,新变量可以从右侧获取隐式类型。另外,例如常量1在Go中也有隐式类型。

英文:

Go is designed with ease of use in mind. So new variables are able to get an implicit type of the right side using the := operator. Also the constant 1 for example has an implicit type in go.

huangapple
  • 本文由 发表于 2016年3月29日 15:33:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/36278121.html
匿名

发表评论

匿名网友

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

确定