The use of go language constants

Constants are defined with the const keyword and are used to store data that will not change. The data types of
constants are generally bool, numbers and strings
.
quote
const {name} {type} = {value} 或 const {name} = value

Multiple definitions are also possible
quote
const {name1},{name2} {type} = {value1},{value2}

Explanation with columns:
package main

import (
	"fmt"
	"reflect"
)

const cK string = "string const"
const cj = "string j"
const (
	a int = 1
	b
	c int = iota
	d
)

const (
	e, f, g = 5, "6", 7
)

func main() {
	fmt.Printf("cK:%s,cj:%s,a:%d,b:%d,c:%d,d:%d,e:%d,f:%s,g:%d\n", cK, cj, a, b, c, d, e, f, g)
	fmt.Println(reflect.TypeOf(f))
}

Result:



Note: iota is a constant counter of golang language, and iota can only be used in constant expressions. When
the const keyword appears, iota will be reset to 0 (before the first line inside const) The
reflect package is the reflection package of go , reflect.TypeOf reflection data type

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327034404&siteId=291194637