0%

Go_Learning_Fundamental_Constant

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
    true
  • Named constant: is the constant represented by a name. For example:

    1
    2
    3
    4
    const a = "Hello World"
    const b = 100
    const c = 100.0
    const d = true

    Declare 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
    13
    package 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
    20
    package 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 int

    Multiple const

Multiple constants are grouped into a block. Using the flow syntax to decalre multiple constant variables.

1
const ()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package main

import "fmt"

func main() {
fmt.Println("Multiple const is declared with syntax of `const()`")

const (
NAME = "QINGZHI"
AGE = 20
ISMALE = true
HIGHT = 175.8
)
fmt.Printf("consts is %v, %v, %v, %v \n", NAME, AGE, ISMALE, HIGHT)
}
// Multiple const is declared with syntax of `const()`
// consts is QINGZHI, 20, true, 175.8

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

String Constants

  • Go support two type of string literal, the “” double-quote style and the `` back-quote;

Boolean Constants

  • Defination: two untyped constants true and false.

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
    12
    85         /* 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
    5
    3.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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package main

import "fmt"

func main() {
fmt.Println("Default type of a literal constant is determined by its literal form`")

const (
literalString = "Hello World"
literalBool = true
literalInteget = 100
literalRune = 'a'
litaralFloat = 100.0
literalComplex = 5 + 6i
)
fmt.Printf("literalString %T \nliteralBool %T \nliteralInteget %T \nliteralRune %T \nlitaralFloat %T \nliteralComplex %T \n",
literalString, literalBool, literalInteget, literalRune, litaralFloat, literalComplex)
}

// Default type of a literal constant is determined by its literal form`
// literalString string
// literalBool bool
// literalInteget int
// literalRune int32
// litaralFloat float64
// literalComplex complex128

Literal constant type is untype

Literal constant type is untype means the type is flexable, could transfer to similer type;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package main

import (
"fmt"
)

func main() {
const a = 5
var intVar int = a
var int32Var int32 = a
var float64Var float64 = a
var complex64Var complex64 = a
fmt.Println("intVar", intVar, "\nint32Var", int32Var, "\nfloat64Var", float64Var, "\ncomplex64Var", complex64Var)
}

// intVar 5
// int32Var 5
// float64Var 5
// complex64Var (5+0i)