SoFunction
Updated on 2025-03-04

Constants in GO

Constants are the most basic elements in the program and cannot be reassigned after definition. Constant types in Go language include boolean constants, integer constants, floating point constants, character constants, string constants and complex constants.

Boolean constants

Copy the codeThe code is as follows:

const x = true
(x) //Output true

Integer constants

Copy the codeThe code is as follows:

const x = 20
(x) //Output 20

Floating point constant

Copy the codeThe code is as follows:

constx = 0.618
(x) //Output %f0.618

Character constants

Copy the codeThe code is as follows:

const x = 'a'
(x) //Output 97

String constants

Copy the codeThe code is as follows:

const x = "a"
(x) //Output a

Complex constants

Copy the codeThe code is as follows:

const x = 3 + 2i
(x) //Output %v(3+2i)

If you look closely, you will find that the output values ​​of 'a' and 'a' are different. The single quoted 'a' represents a character, and the double quoted 'a' represents a string. In Go language, '1', "1" and 1 are different values, which are the same as C language. If you are interested, you can try the output of children's boots by yourself.

The above is the entire content of this article, I hope you like it.