Defining constants has become an indispensable syntax in every language. Some languages add enumerated types to constants, such as C.

enum Weekday {
        SUNDAY,
        MONDAY,
        TUESDAY,
        WEDNESDAY,
        THURSDAY,
        FRIDAY,
        SATURDAY
};
Copy the code

Enumerations above, with values 0 through 6.

In Go, there is no enumeration type. If you implement the same function as above, why write it like this?

const (
	SUNDAY = 0
	MONDAY = 1
	TUESDAY = 2
	WEDNESDAY = 3
	THURSDAY = 4
	FRIDAY = 5
	SATURDAY = 6
)
Copy the code

But if you think about it, one of the developers of Go is the father of THE C language, how could you not inherit such a beautiful syntax?

Go implements two special features on constant definitions:

  1. If a constant is not assigned, the default is to repeat the previous line
  2. Iota artifact

Tell me about each of these guys.

Default values for constants

When a constant has no explicit value, it defaults to repeating the behavior assigned to the previous line. Such as:

const (
	a    = 1
	b    / / 1
	c    = 2
	d    / / 2
	e, f = 3.4
	g, h / / 3, 4
	i    // error because the previous line was assigned 3,4. Cannot be repeated
)
Copy the code

Including if the previous one specified the type, the following one will also be specified.

iota

Is the index value (line number) of the current constant block, starting at 0. A definition of a constant block that represents the constant definition of a package in const(). Such as:

const s = iota / / 0
const o = iota / / 0
const (
	a    = iota       / / 0
	b                 / / 1
	c                 / / 2
	d, e = iota.iota / / 3, 3
	f, g              / / 4, 4
)
Copy the code

In constant definitions, IOTA is allowed to participate in operations.

use

To name a few examples I can think of, it is often possible to combine the above features to achieve good results.

1. Define the binary identifier

const (
	isA = 1 << iota // 0b1
	isB // 0b10
	isC // 0b100
	isD // 0b1000
)
Copy the code

2. Discard and reuse IOTA

When you want the index to start at 1, you can drop the first row.

const(_ =iota / / 0
	a / / 1
	b / / 2
	_ / / 3
	c,d = iota.iota // 4
)
Copy the code

3. Numerical operations

The above are relatively simple, want to play a few more flower. To Chou Chou.

const (
	P   = 3.1415926      / / define a PI
	P_2 = P / (2 * iota) // P/2
	P_4                  // P/4
	P_6                  // P/6
	P_8                  // P/8
)
Copy the code
const (
	P    = 3.1415926       / / PI.
	P_2  = P / (1 << iota) // P/2
	P_4                    // P/4
	P_8                    // P/8
	P_16                   // P/16
)
Copy the code
const (
	P          = 3.1415926                       / / row
	P_2, P_M_2 = P / (1 << iota), P * (iota + 1) // P/2 P*2
	P_4, P_M_3                                   // P/4 P*3
	P_8, P_M_4                                   // P/8 P*4
)
Copy the code

4. Calculate the capacity unit

const(_ =1< < (10 * iota) // Index 0 is not needed
	KB                    / / 2 ^ 10
	MB                    / / 2 ^ 20
	GB                    / / 2 ^ 30
	TB                    / / 2 ^ 40 similarly hereinafter
	PB
	EB
	ZB
	YB
)
Copy the code

OK, Go constant definition is basically that much, if there is no IOTA, then there is no point in repeating the previous line of behavior, after all, what is the use of defining several constants with the same value? When you combine these two features, you can often achieve great results.