Reference: Blog_1, Blog_2, Blog_3, Blog_4, Blog_5, Blog_6, Mind
Summary
Defination
A Constant is a value that can’t be changged during program life cycle; This is a contrasted concept with a variable which can be changed during program running;
Difference of Constant And Variable
- Constant: the value is constant, value can not be change during running time;
- Variable: the value can vary, value can change during running time;
Literal constant And Defined constant
Constants are used in two ways: literal constant, named constant; When associated with an identifier, a constant is said to be named, the terms “constant” and “named constant” are ofen used interchangeably;
Literal constant (Unnamed constant) : is the value which typed into program. For example:
1
2
3
4"Hello World"
100
100.0
trueNamed constant: is the constant represented by a name. For example:
1
2
3
4const a = "Hello World"
const b = 100
const c = 100.0
const d = trueDeclare Constant in Go
In Go language, const
is the keyword to declare a constant;
Typed && Untyped Const
Constants can be declared with or without a type in Go;
Syntax of Typed constant:
1
const variableName type = value
Typed constant work like immutable variables can inter-operate only with the same type;
1
2
3
4
5
6
7
8
9
10
11
12
13package main
import "fmt"
func main() {
fmt.Println("Typed const can operate with same type")
const typedNum int = 10
var b int = 20
result1 := b + typedNum
fmt.Printf("typedNum + int result %v , result type %T \n", result1, result1)
// var a float64 = 20.0
// result := a + typedNum Error: mismatch types
}Syntax of UnTyped constant:
1
const variableName = value
UnTyped constant work like literals can inter–operate with similar types;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20package main
import "fmt"
func main() {
fmt.Println("Untyped const can operate with similer type")
const untypedNum = 10
var a float64 = 20.0
result := a + untypedNum
fmt.Printf("untypedNum + float64 result %v , result type %T \n", result, result)
var b int = 20
result1 := b + untypedNum
fmt.Printf("untypedNum + int result %v , result type %T \n", result1, result1)
}
// Untyped const can operate with similer type
// untypedNum + float64 result 30 , result type float64
// untypedNum + int result 30 , result type intMultiple const
Multiple constants are grouped into a block. Using the flow syntax to decalre multiple constant variables.
1 | const () |
1 | package main |
Feature
- Upppercase letters are most preferred to represent a constant variable;
- Constant variables are declared inside as well as outside of a function;
- Constant variable can not be redeclared;
- The value of a constant should be known at complie time;
Various Types of Constant
Three Types of constants in Go
- Reference by Blog
String Constants
- Go support two type of string literal, the “” double-quote style and the `` back-quote;
Boolean Constants
- Defination: two untyped constants
true
andfalse
.
Numeric Constants
Defination: three type of numeric constants:
integers
,floats
,complex
;integer Constant:
- A prefix specifies the base or radix: 0x or 0X for hexadecimal, 0 for octal, and nothing for decimal.
- An integer literal can also have a suffix that is a combination of U(upper case) and L(upper case), for unsigned and long, respectively
- It can be a decimal, octal, or hexadecimal constant.
- An int can store at maximum a 64-bit integer, and sometimes less.
1
2
3
4
5
6
7
8
9
10
11
1285 /* decimal */
0213 /* octal */
0x4b /* hexadecimal */
30 /* int */
30u /* unsigned int */
30l /* long */
30ul /* unsigned long */
212 /* Legal */
215u /* Legal */
0xFeeL /* Legal */
078 /* Illegal: 8 is not an octal digit */
032UU /* Illegal: cannot repeat a suffix */Complex constant
Floating type constant
- A floating type constant has an integer part, a decimal point, a fractional part, and an exponent part.
- Can be represent floating constant either in decimal form or exponential form.
- While representing using the decimal form, you must include the decimal point, the exponent, or both.
- And while representing using the exponential form, must include the integer part, the fractional part, or both.
1
2
3
4
53.14159 /* Legal */
314159E-5L /* Legal */
510E /* Illegal: incomplete exponent */
210f /* Illegal: no decimal or exponent */
.e55 /* Illegal: missing integer or fraction */Default type of literal constant
Default type of literal constant is determined by its literal form:
- The default type of a string literal is
string
. - The default type of a boolean literal is
bool
. - The default type of an integer literal is
int
. - The default type of a rune literal is
rune
(a.k.a.,int32
). - The default type of a floating-point literal is
float64
. - If a literal contains an imaginary part, then its default type is
complex128
.
1 | package main |
Literal constant type is untype
Literal constant type is untype
means the type is flexable, could transfer to similer type;
1 | package main |