[Go] Constant

iota

  • Generate a set of related but distinct constants
  • Often represents a property which has several distinct possible values
    • Dyas of the week
    • Months of the year
package main

import "fmt"

func main() {
	type Grades int
	const (
		A Grades = iota
		B
		C
		D
		E
	)

	fmt.Println(A) // 0
	fmt.Println(B) // 1
}

 

To skip zero

package main

import "fmt"

func main() {
	type Grades int
	const (
		_ Grades = iota
		A
		B
		C
		D
		E
	)

	fmt.Println(A) // 1
	fmt.Println(B) // 2
}

 

posted @ 2022-09-08 15:08  Zhentiw  阅读(14)  评论(0编辑  收藏  举报