WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

Go - Constants


A constant is an identifier of a simple value that is not modified when the program is run.

Data types in constants can only be boolean, numeric (integer, float, and complex) and string.

The definition format of the constant:

const identifier [type] = value

You can omit the type specifier [type] because the compiler can infer its type based on the value of the variable.

  • Explicit type definition: const b string = "abc"
  • Invisible type definition: const b = "abc"

Multiple statements of the same type can be abbreviated as:

const c_name1, c_name2 = value1,  value2

The following example demonstrates the application of constants:

The following example demonstrates the application of constants:

example

package main

import "fmt"

func main() {
   const LENGTH int = 10
   const WIDTH int = 5  
   var area int
   const a, b, c = 1, false, "str" //Multiple assignment

   area = LENGTH * WIDTH
   fmt.Printf("Area is : %d", area)
   println()
   println(a, b, c)  
}

The above example runs as:

area : 50
1 false Str
The

constant can also be used as an enumeration:

const (
    Unknown = 0
    Female = 1
    Male = 2
)

Numbers 0, 1, and 2 represent unknown gender, women, and men, respectively.

Constors You can use the len(), cap(), unsafe.Sizeof() functions to evaluate the value of an expression. In a constant expression, the function must be a built-in function, otherwise it will compile:

example

package main

import "unsafe"
const (
    a = "abc"
    b = len(a)
    c = unsafe.Sizeof(a)
)

func main(){
    println(a, b, c)
}

The above example runs as:

abc 3 16

iota

iota, a special constant, can be thought of as a constant that can be modified by the compiler.

iota will be reset to 0 when the const keyword appears (before the first line inside const), and every new line of constant declaration in const will cause iota to count once (iota can be understood as a line in a const statement block) index).

iota can be used as an enumeration value:

const (
     a = iota
     b = iota
     c = iota
)

The first iota is equal to 0. Whenever iota is used on a new line, its value is automatically incremented by one; so a=0, b=1, c=2 can be abbreviated as follows:

const (
    a = iota
    b
    c
)

iota usage

example

package main

import "fmt"

func main() {
    const (
            a = iota   //0
            b          //1
            c          //2
            d = "ha"   //Independent value,iota += 1
            e          //"ha"   iota += 1
            f = 100    //iota +=1
            g          //100  iota +=1
            h = iota   //7,Recovery count
            i          //8
    )
    fmt.Println(a,b,c,d,e,f,g,h,i)
}

The above example runs as follows:

0 1 2 ha ha 100 100 7 8

Look at an interesting iota instance:

Instance

package main

import "fmt"
const (
    i=1<<iota
    j=3<<iota
    k
    l
)

func main() {
    fmt.Println("i=",i)
    fmt.Println("j=",j)
    fmt.Println("k=",k)
    fmt.Println("l=",l)
}

The above example runs as:

i= 1
j= 6
k= 12
l= 24

iota Indicates that it is automatically incremented from 0, soi=1<<0, j=3<<1<< Indicates the meaning of shifting to the left), ie: i=1, j=6, which is ok, the key is in k and l, from the output result k=3<<2l=3<<3

Simple representation:

    i=1: Shift left 0, still unchanged 1;
  • j=3: Move 1 bit to the left and change to binary 110, which is 6;
  • k=3: Move 2 bits to the left and change to binary 1100, which is 12;
  • l=3: Move 3 bits to the left and change to binary 11000, which is 24.

Integer Literals

An integer literal can be a decimal, octal, or hexadecimal regular. A prefix specifies the base or radix: 0x or 0X for hexadecimal, zero for octal, and nothing for decimal.

Constants can be of any of the basic data types like an integer constant, a floating constant, a character constant, or a string literal.

Here are a few examples of integer literals
212         /* Legal */
215u        /* Legal */
0xFeeL      /* Legal */
078         /* Illegal: eight isn't an octal digit */
032UU       /* Illegal: can not repeat a suffix */

Floating-point Literals

A floating-factor literal has an integer part, a decimal point, a fractional element, and an exponent part. The signed exponent is brought by way of e or E.
3.14159       /* Legal */
314159E-5L    /* Legal */
510E          /* Illegal: incomplete exponent */
210f          /* Illegal: no decimal or exponent */
.E55          /* Illegal: lacking integer or fraction */

Escape Sequence

When certain characters are preceded by a backslash, they will have a new character meaning in Go. Sequence codes that are used to represent newline (n), tab (t), backspace, etc. Here, you have a list of some of such escape sequence codes −
Escape series Meaning
individual
' ' individual
" " man or woman
? ? Person
a Alert or bell
b Backspace
f Form feed
n Newline
r Carriage return
t Horizontal tab
v Vertical tab
ooo Octal range of one to a few digits
xhh . . . Hexadecimal variety of 1 or more digits

The following example suggests how to use t and " in a application

package deal essential

import "fmt"

func important() 
   fmt.Printf("HellotWorld! "welookups" ")



Output

$pass run foremost.Pass
Hello	World! "Welookups" 

String Literals in Go

String literals or constants are enclosed in double charges "".A string includes characters that are much like character literals: characters, escape sequences, and universal characters.

Here are some examples of string literals. All the three forms are identical strings.

"hello, dear"

"hello, \

dear"

"hello, " "d" "ear"

The const Keyword

You can use const prefix to claim constants with a specific type as follows
const variable type = value;
The following example shows the way to use the const keyword
package main

import "fmt"

func main() {
   const LENGTH int = 10
   const WIDTH int = 5   
   var area int

   area = LENGTH * WIDTH
   fmt.Printf("value of area : %d", area)   
}

When the above code is compiled and executed, it produces the following result −

value of area : 50

Note that it is a good programming practice to define constants in CAPITALS.