Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

The value of a constant expression is evaluated at compile time, not run time. The underlying type of each constant is the base type: Boolean, String, or number.

A constant declaration statement defines the name of a constant, similar to the declaration syntax for variables. The value of a constant cannot be modified, which prevents accidental or malicious modification at run time. Constants, for example, are better than variables for expressing mathematical constants like π because their values do not change:

Const PI = 3.14159 // approximately; math.Pi is a better approximationCopy the code

As with variable declarations, multiple constants can be declared in batches; This is a good way to declare a set of related constants:

PI = const (e = 2.71828182845904523536028747135266249775724709369995957496696763 3.14159265358979323846264338327950288419716939937510582097494459)Copy the code

All constant operations can be done at compile time, which reduces run-time effort and facilitates other compilation optimizations.

Iota constant generator

Constant declarations can be initialized using the IOTA constant generator, which generates a set of constants initialized with similar rules, but without having to write an initialization expression for each line. In a const declaration, ioTA is set to 0 on the line where the first constant is declared, and then incremented by one on each line where the constant is declared.

type Weekday intconst (
    Sunday Weekday = iota
    Monday
    Tuesday
    Wednesday
    Thursday
    Friday
    Saturday
)
Copy the code

It first defines a Weekday named type, and then a constant for each day of the week, starting at Sunday 0. In other programming languages, this type is commonly referred to as an enumerated type.

Sunday will correspond to 0, Monday to 1, and so on.

Untyped constant

Although a constant can have any identified base type, such as int or float64, or a base type named like time.Duration, many constants do not have an explicit base type. The compiler provides higher precision arithmetic for these numeric constants without an explicit base type; You can think of it as at least 256bit precision. There are six untyped constant types: untyped Booleans, untyped integers, untyped characters, untyped floating-point numbers, untyped complex numbers, and untyped strings.

By delaying the exact type of a constant, untyped constants not only provide higher computational accuracy, but can be used directly in more expressions without explicit type conversions.

fmt.Println(YiB/ZiB) // "1024"
Copy the code

As another example, math.Pi untyped floating-point constants can be used directly wherever floating point or complex numbers are required:

var x float32 = math.Pi
var y float64 = math.Pi
var z complex128 = math.Pi
Copy the code

– END –

Author: The road to architecture Improvement, ten years of research and development road, Dachang architect, CSDN blog expert, focus on architecture technology precipitation learning and sharing, career and cognitive upgrade, adhere to share practical articles, looking forward to growing with you. Attention and private message I reply “01”, send you a programmer growth advanced gift package, welcome to hook up.

Thanks for reading!