Constants in the GO language

  • 2020-05-27 05:51:54
  • OfStack

Constants are the most basic element in a program and cannot be re-assigned after definition. The types of constants in the Go language are Boolean constants, integer constants, floating point constants, character constants, string constants, and complex constants.

Boolean constants

const x = true
fmt.Print(x) // The output true

Integer constants

const x = 20
fmt.Print(x) // The output 20

Floating point constant

constx = 0.618
fmt.Print(x) // The output %f0.618

Character constants

const x = 'a'
fmt.Print(x) // The output 97

String constant

const x = "a"
fmt.Print(x) // The output a

The plural constants

const x = 3 + 2i
fmt.Print(x) // The output %v(3+2i)

If you look closely at the child's boot, you will find that the output values of 'a' and 'a' are different. The single-quoted 'a' represents characters and the double-quoted 'a' represents strings. In Go, '1 ', "1", 1 is a different value, this is the same as C. If you are interested, you can try the output by yourself.

That's all for this article, I hope you enjoy it.

Related articles: