了解 Golang 中的 iota 和常量

问题描述 投票:0回答:1

有人可以解释一下这行代码发生了什么吗:

type Fate int

const (
Decided   Fate = iota + 1
Pending        // not yet decided.
Forgotten      // decided but forgotten.
)

我不明白“决定”、“待定”和“遗忘”应该是什么。

go integer constants iota
1个回答
0
投票

const
块中,每个连续的iota代表递增的整数常量。如果您不提供初始化常量的表达式,则最后一个表达式会隐式重复,因此您的代码相当于:

const {
Decided   Fate = iota + 1 // 1
Pending   Fate = iota + 1 // 2
Forgotten Fate = iota + 1 // 3
)

https://go.dev/ref/spec#Iota

© www.soinside.com 2019 - 2024. All rights reserved.