English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
As the name “ CONSTANTS”The implied meaning is fixed, just like in programming languages, that is, once the value of a constant is defined, it cannot be modified. Constants can have any basic data type, such as integer constants, floating-point constants, character constants, or string literals.
How to declare?
Constants are declared like variables, but they useconst Keywords are used as prefixes to declare constants of specific types. They cannot be:=Syntax declaration.
package main import "fmt" const PI = 3.14 func main() { const GFG = "w"3codebox" fmt.Println("Hello", GFG) fmt.Println("Happy", PI, "Day") const Correct = true fmt.Println("Go rules?", Correct) }
Output:
Hello w3codebox Happy 3.14 Day Go rules? true
Untyped and typed numeric constants:
The way typed constants work is like immutable variables that can only operate with the same type, and the way untyped constants work is like literal constants that can operate with similar types. Constants can be declared with or without types in Go. The following examples show named and unnamed typed and untyped numeric constants.
const untypedInteger 123 const untypedFloating typed 123.12 const typedInteger int 123 const typedFloatingPoint float64 = 123.12
The following is a list of constants in the Go language:
Numeric constants (integer literals, floating-point literals, complex literals)
String literals
Boolean constants
Numeric constants:
Numeric constants areHigh precision values. Go is a statically typed language and does not allow operations on mixed numeric types. You cannot addfloat64added toint, and even cannot be added toint32add toint. Although, writing1e6 * time.Second ormath.Exp(1)Even 1 <<('\t')+ 2.0) They are all legal. In Go, constants are different from variables, and their behavior is similar to that of regular numbers.
Numeric constants can be3Types, namely integers, floating-point numbers, and complex numbers
Integer constants:
Prefix specifying the base: hexadecimal is 0x or 0X, octal is 0, decimal is 0.
Integer literals can also haveSuffixwhich are combinations of U (uppercase) and L (uppercase), respectively indicating unsigned and long integers.
It can beDecimal, octal, or hexadecimal constants.
An int can store up to64bit integer, sometimes less.
The following areInteger constantsSome examples:
85 /* Decimal */ 0213 /* Octal */ 0x4b /* Hexadecimal */ 30 /* int */ 30u /* unsigned int */ 30l /* long */ 30ul /* unsigned long */ 212 /* Valid */ 215u /* Valid */ 0xFeeL /* Valid */ 078 /* Illegal:8is not an octal digit */ 032UU /* Illegal: cannot repeat suffix */
Complex constant:
The behavior of complex constants is very similar to that of floating-point constants. It is an integer constant (or parameter)ordered pair or real number pairand the constant is separated by commas, and the pair is enclosed in parentheses. The first constant is the real part, and the second constant is the imaginary part. The complex constant COMPLEX * 8using8bytesstorage space.
(0.0, 0.0) (-123.456E+30, 987.654E-29)
Floating type constants:
Floating-point constants have aAn integer part, a decimal point, a fractional part, and an exponent part.
Floating-point constants can be expressed in decimal or exponential form.
AlthoughDecimal form must include a decimal point, exponent, or both.
and when usingExponentWhen represented in the form, an integer part, a fractional part, or both must be included.
Here are examples of floating-point type constants:
3.14159 /* Valid */ 314159E-5L /* Valid */ 510E /* Invalid: incomplete exponent */ 210f /* Invalid: no decimal or exponent */ .e55 /* Invalid: missing integer or fraction */
String literals
Go supports two types of string literals, i.e., "" (double quote style) and "" (backtick).
Strings canchainingwith+and+ =Operators.
Strings contain characters similar to character literals: plain characters, escape sequences, and universal characters, which are untyped.
The zero value of the string type is an empty string, which can be represented by or with a literal. " " ''
By using==, !=and (used for comparing the same type) etc. operators,Comparable string types
Syntax
type _string struct { elements *byte // Underlying bytes len int //Byte count }
Examples of displaying string literals:
"hello, w3codebox"
"hello, \
w3codebox"
"hello, " "geeks" "forgeeks"
Here, all three statements above are similar, i.e., they have no specific type.
Example:
package main import "fmt" func main() { const A= "GFG" var B = "w3codebox" //String concatenation var helloWorld = A+ " " + B helloWorld += \ fmt.Println(helloWorld) //Comparing strings fmt.Println(A == "GFG") fmt.Println(B < A) }
Output:
GFG w3codebox! true false
Boolean constants:
Boolean constants are similar to string literals. They follow the same rules as string literals, the difference being that they have two untyped constants, true and false.
package main import "fmt" const Pi = 3.14 func main() { const trueConst = true type myBool bool var defaultBool = trueConst // Allowed var customBool myBool = trueConst // Allowed // defaultBool = customBool // Not allowed fmt.Println(defaultBool) fmt.Println(customBool) }
Output:
true true