WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

Go - Constants


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

Integer Literals

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

Here are some examples of integer literals
212         /* Legal */
215u        /* Legal */
0xFeeL      /* Legal */
078         /* Illegal: 8 is not an octal digit */
032UU       /* Illegal: cannot repeat a suffix */

Floating-point Literals

A floating-point literal has an integer part, a decimal point, a fractional part, and an exponent part. The signed exponent is introduced by e or E.
3.14159       /* Legal */
314159E-5L    /* Legal */
510E          /* Illegal: incomplete exponent */
210f          /* Illegal: no decimal or exponent */
.e55          /* Illegal: missing integer or fraction */

Escape Sequence

Sequence codes which are used to represent newline (\n), tab (\t), backspace, etc.
Escape sequence Meaning
\\ \ 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

The following example shows how to use \t and \" in a program

package main

import "fmt"

func main() {
   fmt.Printf("Hello\tWorld! \"welookups\" ")
}


Output

$go run main.go
Hello	World! "Welookups" 

String Literals in Go

String literals or constants are enclosed in double quotes "".A string contains characters that are similar to character literals: plain characters, escape sequences, and universal characters.

"hello, MASTER"

"hello, \

dear"

"hello, " "M" "ASTER"

The const Keyword

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

import "fmt"

func main() {
   const LENGTH int = 12
   const WIDTH int = 6  
   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
$go run main.go
value of area : 72