1024programmer Java 05-GoLang constants

05-GoLang constants


1. In C language, constants can be defined through const

2. Same as in Go language

3. Format: const variable name data type = value


  • The data type can be omitted, but const cannot be omitted
  • You cannot use := to define constants, := is specially used to define local variables
  • If local variables are defined but not used, an error will be reported during compilation. If global variables and constants are defined but not used, no error will be reported
  • In the Go language, multiple variables can be defined continuously, so multiple constants can also be defined continuously
    const variable name1, variable name2 data type = value1, value2;
    Variable name 1 data type = value 1
    Variable name 2 data type = value 2
    Define a single constant
const num int = 666 //Method 1
     //const num = 666 //Method 2
     //const num := 666 error, := can only be used to define local variables
     fmt.Printf("%d\n", num)

Define multiple constants

const a, b, c int = 10, 20, 30 //Method 1
     //const a, b, c = 10, 20, 30 //Method 2
     //Constant is not a variable. It can only be initialized at the same time when it is defined. It cannot be assigned a value later. An error will be reported in the next three lines of code.
     //a = 666
     //b = 777
     //c = 888

Constant group

//Method 1
         a int = 10
         b int = 20
         c int = 30
 //Method 2
     // a = 10
     // b = 20
     // c = 30
     //The next three lines will report an error
     //a = 666
     //b = 777
     //c = 888

Notes on constant groups

  • Unlike variables, defined constants are not used, the compiler will not report an error and can be compiled and run
  • In the constant group, if the constant is not assigned a value, the default value is the value of the previous line. Note that it is one line!
         a = 666 //666
         b = 789 //789
         c //789
     fmt.Printf("%d, %d, %d", a, b, c)
         a, b = 10, 20
         c, d //c, d correspond to the values ​​​​of a, b
     fmt.Printf("%d, %d\n", a, b) //10,20
     fmt.Printf("%d, %d\n", c, d) //10,20

Enum constants

Enumeration in C language

  • The essence of an enumeration in C language is an integer
  • Format:
    enum enumeration name {
    Enum element 1 = value,
    enum element 2 = value,
    Enumerations in Go language
  • There is no fixed way of writing explicit enumerations in Go statements, but in enterprise development, enumerations are generally expressed in the form of constant groups
  • Format:
    enum element = value
    enum element = value
         male = 0
         female = 1
  • Iterator iota
//iota is an iterator, which will increase from 0 by default
         female = iota
         yao = iota
  • As long as iota appears in the constant group, subsequent constants in the constant group will be incremented by 1 at a time
         male = iota //0
         female //1
         Yao //2
  • If iota in the constant group is interrupted, it will not continue to increment and will be processed according to the default method of the constant group (the value of the previous line)
         male = iota //0
         female = 666 //666
         yao //666
  • If itoa in the constant group is interrupted, but then restored, the number of previous lines will be incremented
         male = iota //0
         female = 666 //666
         yao = iota //2

The above is the entire content of this article. I hope that the content of this article can bring some help to everyone’s study or work, and I also hope that everyone will support us

This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/754401

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

Contact us


Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top