"Go Language Lesson 1" Course Study Notes (9)

Constants: What innovations does Go have in the design of "constants"?

  • The innovations of the Go language in terms of constants include the following:
    • Support for untyped constants;
    • Support for implicit auto-transformation;
    • Can be used to implement enums.

constant

  • A constant in Go is a syntax element created during source code compilation. This is to say that the value of this element can be initialized like a variable, but its initialization expression must be able to evaluate the value during compilation.
    • Once a Go constant is declared and initialized, its value remains constant throughout the life of the program.
    • We don't need to consider the synchronization of constant access when designing concurrently, and the created and initialized constant can also be used as part of the initial expression of other constants.
  • Go declares variables using the var keyword. For constants, the Go language introduces the const keyword to declare constants. Moreover, just like var supports declaring multiple variables in a single line and aggregating variable declarations in code blocks, const also supports declaring multiple constants in a single line and aggregating constant declarations in code blocks:
    const Pi float64 = 3.14159265358979323846 // 单行常量声明
    // 以 const 代码块形式声明常量
    const (
    	size int64 = 4096
    	i, j, s = 13, 14, "bar" // 单行声明多个常量
    )
    
  • The Go language specification stipulates that the types of Go constants are limited to Go basic data types, including numeric types, string types, and Boolean types with only two values ​​(true and false).

untyped constant

  • The Go language has strict requirements on type safety: Even if two types have the same underlying type, they are still different data types and cannot be compared with each other or mixed in an expression for operation. This requirement applies not only to variables, but also to Typed Constants.
  • When type constants and variables are mixed together for calculation and evaluation, the requirement of the same type must also be complied with, otherwise we can only make the code work properly through explicit transformation.
  • A constant is not explicitly assigned a type when it is declared. In Go, such a constant is called an untyped constant (Untyped Constant).
  • An untyped constant does not mean that it really has no type. It also has its own default type, but its default type is determined according to its initial value form.

implicit conversion

  • Implicit conversion means that for the evaluation of expressions involving untyped constants, the Go compiler will automatically convert the untyped constants into the corresponding type according to the type information in the context, and then participate in the evaluation calculation. Actions are performed implicitly.
  • But since the transformed object is a constant, this does not cause type safety issues, and the Go compiler will guarantee the safety of this transformation.

implement enumeration

  • In fact, the Go language does not provide enumeration types natively. In the Go language, we can use the constant collection defined by the const code block to implement the enumeration.
  • First, Go's const syntax provides a mechanism for "implicitly repeating the previous non-null expression":
    const (
    	Apple, Banana = 11, 22
    	Strawberry, Grape
    	Pear, Watermelon
    )
    
    • In this code, the last two lines of the constant definition are not explicitly assigned an initial value, so the Go compiler automatically uses the expression on the previous line for them:
      const (
      	Apple, Banana = 11, 22
      	Strawberry, Grape = 11, 22
      	Pear, Watermelon = 11, 22
      )
      
    • However, just repeating the previous line obviously cannot satisfy the requirement of "enumeration", because the value of each enumeration constant in the enumeration type is unique. Therefore, Go provides an "artifact" based on this feature: iota.
      • iota is a predefined identifier of the Go language, which represents the offset value (starting from zero) of each constant in the block in the const declaration block (including single-line declaration).
      • At the same time, the iota in each row is also an untyped constant, which can automatically participate in the evaluation process of different types, without us needing to perform explicit transformation operations on it.
        const (
        	Apple, Banana = iota, iota + 10 // 0, 10 (iota = 0)
        	Strawberry, Grape // 1, 11 (iota = 1)
        	Pear, Watermelon // 2, 12 (iota = 2)
        )
        
    • If there are different enumerations defined by multiple const code blocks in a Go source file, the iota in each const code block also changes independently, that is, each const code block has its own iota.
    • The lifetime of each iota begins at the beginning of a const code block and ends when that const code block ends.

Guess you like

Origin blog.csdn.net/fangzhan666/article/details/132402433