Go the constant of language learning

Time:2020-9-15

01 concept

A constant is an expression. The calculation of a constant can be completed at compile time. If there is an error, an error will be reported at compile time. The value of the constant cannot be modified.

02 declaration constant

A constant declaration can specify both a type and a value, and if the specified type is not displayed, the type is inferred from the expression on the right. It should be noted that the declaration of constants is different from the declaration of variables, which can not be assigned, and the declared constants must be assigned values.

const name string = "golang"
const PI = 3.14

func main() {
  fmt.Printf("name = %s type:%T\n", name, name)
  fmt.Printf("PI = %g type:%T\n", PI, PI)
}

Declare a set of constants, each of which can be different.

const (
  x = 1
  y = "hello"
  z = true
)

func main() {
  fmt.Printf("x = %d type:%T\n", x, x)
  fmt.Printf("y = %s type:%T\n", y, y)
  fmt.Printf("z = %t type:%T\n", z, z)
}

Declare a set of constants. Except for the first constant, other constants can omit expressions. Other constants reuse the type and expression of the previous constant.

const (
  a = 1
  b
  c = 2
  d
)

func main() {
  fmt.Printf("a = %d type:%T\n", a, a)
  fmt.Printf("b = %d type:%T\n", b, b)
  fmt.Printf("c = %d type:%T\n", c, c)
  fmt.Printf("d = %d type:%T\n", d, d)
}

03 constant generator iota

To declare a group of constants, you can use the constant generator iota, which does not need to display the specified type and assignment expression one by one. In the constant declaration, the value of iota starts from 0. If the specified type and assignment expression are not displayed for subsequent constants, the value is the value of the previous constant plus 1. Stop adding 1 until the constant of assignment is encountered. If the constant of assignment iota is encountered again, continue to add 1 one by one.

const (
  Sunday int = iota
  Monday
  Tuesday
  Wednesday
  Thursday
  Friday
  Saturday
)

const (
  aa int = iota
  bb
  cc
  dd = 10
  ee
  ff = iota
  gg
  hh
)

func main() {
  fmt.Printf("Sunday = %d type:%T\n", Sunday, Sunday)
  fmt.Printf("Monday = %d type:%T\n", Monday, Monday)
  fmt.Printf("Tuesday = %d type:%T\n", Tuesday, Tuesday)
  fmt.Printf("Wednesday = %d type:%T\n", Wednesday, Wednesday)
  fmt.Printf("Thursday = %d type:%T\n", Thursday, Tuesday)
  fmt.Printf("Friday = %d type:%T\n", Friday, Friday)
  fmt.Printf("Saturday = %d type:%T\n", Saturday, Saturday)
  fmt.Printf("aa = %d type:%T\n", aa, aa)
  fmt.Printf("bb = %d type:%T\n", bb, bb)
  fmt.Printf("cc = %d type:%T\n", cc, cc)
  fmt.Printf("dd = %d type:%T\n", dd, dd)
  fmt.Printf("ee = %d type:%T\n", ee, ee)
  fmt.Printf("ff= %d type:%T\n", ff, ff)
  fmt.Printf("gg = %d type:%T\n", gg, gg)
  fmt.Printf("hh = %d type:%T\n", hh, hh)
}

04 no type constant

If there is no type constant, it can be directly assigned to other constants, and the constant of specified type will be displayed. When assigning to other constants, strong type checking will be performed.

const username, age = "lucy", 17
const user = username

func main() {
  fmt.Printf("username = %s, age = %d\n", username, age)
  fmt.Printf("user = %s\n", user)
}

Go Language Basics
Go the constant of language learning