The constants are nothing but the fixed values that the program may not alter during its execution. These fixed values are also called literals. Var const I = 10;
The constants are treated just like regular variables except that their values cannot be modified after their definition.
Constants can be of any of the basic data types like
The constants are treated just like regular variables except that their values cannot be modified after their definition.
an integer constant,
a floating constant,
a character constant, or
a string literal. There are also enumeration constants as well.
Integer literals
An integer literal can be a
decimal,
octal, or
hexadecimal constant.
A prefix specifies the base or radix:
A prefix specifies the base or radix:
0x or 0X for hexadecimal,
0 for octal, and
nothing for decimal.
Suffix with the combination of U and L, for unsigned and long, respectively.
The suffix can be uppercase or lowercase and can be in any order.
Here are some examples of integer literals:
Following are other examples of various type of Integer literals:
Floating-point literals
A floating-point literal has an integer part, a decimal point, a fractional part, and an exponent part. You can represent floating point literals either in decimal form or exponential form.
While representing using decimal form, you must include the decimal point, the exponent, or both and while representing using exponential form, you must include the integer part, the fractional part, or both. The signed exponent is introduced by e or E.
golang , gopro , google go , golang tutorial , google language , go language , go programming language
Here are some examples of floating-point literals:
golang , gopro , google go , golang tutorial , google language , go language , go programming language
Sample program
golang , gopro , google go , golang tutorial , google language , go language , go programming language
Output
Escape sequence
Below is the list of escape sequence in Go Programming language.
Escape sequence
Meaning
\\
\ character
\'
character
\'
' character
\"
" character
\?
? character
\a
Alert or bell
\b
Backspace
\f
Form feed
\n
Newline
\r
Carriage return
\t
Horizontal tab
\v
Vertical tab
\ooo
Octal number of one to three digits
\xhh .
. . Hexadecimal number of one or more digits
Go program for escape sequence:
Output for golang program :
String literals
String literals or constants are enclosed in double quotes "". A string contains characters that are similar to character literals:
Below is the list of escape sequence in Go Programming language.
plain characters,
escape sequences, and
universal characters.
You can break a long line into multiple lines using string literals and separating them using whitespaces.
Below is an example, all three are identical and provide the same output.
The const Keyword
Constant keyword provides you the option of not modifying the data,
sample go program
Output for the above go program :
golang , gopro , google go , golang tutorial , google language , go language , go programming language
Key points on constants
const declares a constant value.
A const statement can appear anywhere a var statement can.
Constant expressions perform arithmetic with arbitrary precision.
A numeric constant has no type until it’s given one, such as by an explicit cast.
A number can be given a type by using it in a context that requires one, such as a variable assignment or function call. For example, here math.Sin expects a float64.
golang constant structgolang constant mapgolang constant arraygolang constants iotagolang multiple constantsgolang const initializer is not a constantgolang constants naminggolang const enumgolanggoprogoogle gogolang tutorialgoogle languagego languagego programming languagego languagego programming languagegolang tutorialgo googlegoogle gogolanggp languagego programming language bookgolang coursethe go programming languagebest book to learn golanggolang web development tutorialgolang big datago language bookbig data golanggolang booklearn golanggolang programsgo programming language usesgo to google go to googlelearn go programminggoogle and gowhat is go programming languagego google go google go googlewhich programming language used in googlego to the bookgo scripting languagegolang online tutorialgoogle language programminggo language programminggolang iotgo to go to googlewhy go languagego programming language tutorialgo language tutoriallearn go languagegolang jobsgoogle go softwareprogramming language gogo programing languagegolang web frameworkgo for googlego language ide for windowsgoogle golanggolang programminggoogle coding languagegolang projectsgo programming tutorialgoogle go language tutorialgolang debuggergo to the googlegoogle go programming languagegolang classgolang serverprogramming language used by googlegoogle go languagego programminggo programming pdfgolang developmentgolang frameworkgo language examplesgolang tutorial pdflanguage googlego programgo programming language pdf