From b96041b9e122714b7af7c76d8e91149b6df92817 Mon Sep 17 00:00:00 2001 From: asahi Date: Fri, 10 Jan 2025 13:33:00 +0800 Subject: [PATCH] =?UTF-8?q?=E9=98=85=E8=AF=BBgolang=20const=E7=9B=B8?= =?UTF-8?q?=E5=85=B3=E6=96=87=E6=A1=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Golang/Golang Document.md | 64 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) diff --git a/Golang/Golang Document.md b/Golang/Golang Document.md index 4b7a81c..661db2e 100644 --- a/Golang/Golang Document.md +++ b/Golang/Golang Document.md @@ -310,3 +310,67 @@ ok git.kazusa.red/asahi/fuzz-demo 10.360s > #### 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在`()`中声明多个常量时,如首个常量的类型被指定,则后续常量类型可省略,后续类型与首个被指定的类型保持一致。 +