阅读golang const相关文档

This commit is contained in:
asahi
2025-01-10 13:33:00 +08:00
parent b0e7e87e08
commit b96041b9e1

View File

@@ -310,3 +310,67 @@ ok git.kazusa.red/asahi/fuzz-demo 10.360s
> #### new interesting > #### new interesting
> `new interesting`指会扩充code coverage的用例输入在fuzz test刚开始时new interesting数量通常会因发现新的代码路径快速增加然后会随着时间的推移逐渐减少 > `new interesting`指会扩充code coverage的用例输入在fuzz test刚开始时new interesting数量通常会因发现新的代码路径快速增加然后会随着时间的推移逐渐减少
## syntax
### iota
`iota`关键字代表连续的整数变量,`0, 1, 2`,每当`const`关键字出现时其重置为0
其使用示例如下
```go
package main
import "fmt"
const (
ZERO = iota
ONE
)
const TWO = iota
const (
THREE = iota
FOUR
)
func main() {
fmt.Println(ZERO) // 0
fmt.Println(ONE) // 1
fmt.Println(TWO) // 0
fmt.Println(THREE) // 0
fmt.Println(FOUR) // 1
}
```
另外const关键字也支持如下语法
```go
package main
import (
"fmt"
"reflect"
)
type Shiro uint8
const (
ZERO Shiro = iota
ONE
)
const TWO Shiro = iota
const (
THREE Shiro = iota
FOUR
)
func main() {
fmt.Printf("ZERO %d, %s\n", ZERO, reflect.TypeOf(ZERO).Name())
fmt.Printf("ONE %d, %s\n", ONE, reflect.TypeOf(ONE).Name())
fmt.Printf("TWO %d, %s\n", TWO, reflect.TypeOf(TWO).Name())
fmt.Printf("THREE %d, %s\n", THREE, reflect.TypeOf(THREE).Name())
fmt.Printf("FOUR %d, %s\n", FOUR, reflect.TypeOf(FOUR).Name())
}
```
当const在`()`中声明多个常量时,如首个常量的类型被指定,则后续常量类型可省略,后续类型与首个被指定的类型保持一致。