Golang语法无法理解使用类型定义变量。

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

golang syntax not able to understand defining variable using type

问题

类型 ScoreType int
const (
	Food     ScoreType = iota
	Beverage 
	Water
	Cheese
)

有人能告诉我在结构体中使用它表示什么吗?

我们可以直接使用

var Kij int = 0

const (
	Food  int = Kij
	Beverage 
	Water
	Cheese
)

上述两种方式是相同的还是不同的?

英文:
type ScoreType int
const (
	Food     ScoreType = iota
	Beverage 
	Water
	Cheese
)

Can any one tell what does it signify while using in struct?

We can directly use

var Kij int = 0

const (
	Food  int = Kij
	Beverage 
	Water
	Cheese
)

Are above are same or different??

答案1

得分: 1

是的!它们是不同的。
1)第一个可以编译通过,但第二个会引发错误:(类型为int的变量)不是常量

你可以在不声明新类型ScoreType的情况下使用第一个示例。但这是最佳实践,可以提高代码的可读性。

根据你的问题,似乎你对iota不够了解[这完全没问题]。我认为在这里解释它不是一个好主意,因为互联网上有很多很好的解释:

https://yourbasic.org/golang/iota/

https://yourbasic.org/golang/bitmask-flag-set-clear/

这两个链接将帮助你理解iota背后的思想和它给你带来的力量。祝你好运。

英文:

Yes! they are different .

  1. the first one get compiled but second one raises error : (variable of type int) is not constant.

you can use the first example without declaring a new type ScoreType . but it's a best practice and increases your code readability .

based on your question it seems you don't have enough understanding about iota [ which is totally fine ] .I don't think it's a good idea to explain it here because there are a lot of great explanation on the internet :

https://yourbasic.org/golang/iota/
and
https://yourbasic.org/golang/bitmask-flag-set-clear/

these two links will help you grasp the idea behind iota and the power it gives you . good luck with them .

答案2

得分: 0

第一个编译通过,第二个是编译时错误,所以它们不可能是相同的。你不能使用变量来初始化常量!

第一个示例将ScoreType(0)赋值给FoodScoreType(1)赋值给Beverage,以此类推。每行中的iota值递增。引用自规范:Iota

常量声明中,预声明的标识符iota表示连续的无类型整数常量。它的值是该常量声明中相应ConstSpec的索引,从零开始。

测试代码如下:

fmt.Println(Food)
fmt.Println(Beverage)
fmt.Println(Water)
fmt.Println(Cheese)

输出结果为(在Go Playground上尝试):

0
1
2
3

在第二个示例中,如果你使用const Kij int = 0而不是var,它将编译通过,但会将0赋值给所有常量:Kij不会在每行递增。上述打印语句将输出(在Go Playground上尝试):

0
0
0
0
英文:

The first one compiles, the second is a compile-time error, so they can't possibly be the same. You can't use a variable to initialize a constant!

The first one will assign ScoreType(0) to Food, ScoreType(1) to Beverage etc. The value of iota is incremented on each line. Quoting from Spec: Iota:

> Within a constant declaration, the predeclared identifier iota represents successive untyped integer constants. Its value is the index of the respective ConstSpec in that constant declaration, starting at zero.

To test it:

fmt.Println(Food)
fmt.Println(Beverage)
fmt.Println(Water)
fmt.Println(Cheese)

Which outputs (try it on the Go Playground):

0
1
2
3

In the second example if you'd use const Kij int = 0 instead of var, it would compile, but would assign 0 to all constants: Kij is not incremented on each line. The above print statements will output (try it on the Go Playground):

0
0
0
0

huangapple
  • 本文由 发表于 2022年9月10日 19:24:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/73671404.html
匿名

发表评论

匿名网友

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

确定