Go Constants

Go Language - Constants

Constants are the fixed values in the program which cannot be changed or altered while the program is in execution. It can be also known as the literals. These constants can be in any data types such as integer, float, character, boolean or a string. It can also be of enumeration constant.

Constants are declared with the keyword const and it cannot be declared with the syntax :=. It can be treated like the other regular variables but cannot be changed once it is defined.

Example :

CODE/PROGRAM/EXAMPLE
package main
import "fmt"
const Pi = 3.14	//Global numeric constant
func main() {
	const value = "Bookofnetwork"	//Local string constant
	const Truth = true	//Boolean constant
	fmt.Println("Hello", value)
	fmt.Println(Pi)
	fmt.Println("Boolean check” Truth)
	}
	
Output :
Hello Bookofnetwork
3.14
Boolean check true

Integer Literals

An Integer constant can be of decimal, octal or hexadecimal values. It can have a suffix for long and unsigned as L and U respectively. The suffix can be either in lowercase or in uppercase and can also be in any order but cannot be repeated. It will have a prefix which specifies the radix or base:

  • 0X or 0x − hexadecimal
  • 0 − octal
  • Nothing for decima

Integer constants are high-precision values. If the constant is declared without the type, it

Example :

  • 55 - decimal
  • 0264 - octa
  • 0x8c - hexadecimal
  • 38u - unsigned int
  • 50 - int
  • 45l - long
  • 55ul - unsigned long

Floating-point Literals :

A floating point literal has several parts such as an integer, a decimal point, a fraction and an exponent part. The floating-point literals can be represented two forms like, decimal form or an exponential form.

The decimal point, exponent or both must be included when represented in the decimal form and the integer part, fractional part or both must be included when represented in the exponential form. Signed exponent is represented by e or E.

Example :

  • 3.14159 - decimal form
  • 314159E-5L - exponential form

String Literals :

A String can have different character literals like plain characters, universal characters and escape sequences. String constants or literals must be enclosed within double quotes (“”). A long line can be separated with whitespaces and can also bebroken into multiple line with the string literals.

Example :

“Hi, Bookofnetwork!”

Escape Sequence

In Go the characters preceded by a backslash will have a special meaning and these characters are called Escape Sequence codes. It can be used to represent the tab( ), newline( ), backspace, etc.

go-language-escape-sequence

Example :

CODE/PROGRAM/EXAMPLE
package main
import "fmt"
func main() {
	fmt.Printf("Hello\tBookofnetwork!")	//Horizontal Tab
	}
	
output:
Hello Bookofnetwork!

const Keyword :

The const Keyword is used to declare the constants with a specific types. The syntax for the constkeyboard is as follows,

Syntax
const variable type = value;
Notepad

Note : Defining the constants in CAPS is the good programming practice.

CODE/PROGRAM/EXAMPLE
package main
import "fmt"
func main() {
	const LENGTH int = 10	/*defining constants*/
	const WIDTH int = 5
	var area int
	area = LENGTH * WIDTH
	fmt.Printf("Area: %d", area)   
}

output:
Area: 50
#golang_constants #golang_const #golang_const_string #golang_constant_string #golang_string_const #golang_constants_example #go_language_Escape_Sequence #golang_Escape_Sequence #go_language_const_Keyword #golang_const_Keyword

(New page will open, for Comment)

Not yet commented...