golang tutorial - Golang Const | GO - Constants - golang - go programming language - google go - go language - go program - google language



Golang Const

  • The constants are nothing but the fixed values that the program may not alter during its execution. These fixed values are also called literals. Var const I = 10;
  • The constants are treated just like regular variables except that their values cannot be modified after their definition.

Constants can be of any of the basic data types like

  • The constants are treated just like regular variables except that their values cannot be modified after their definition.
    • an integer constant,
    • a floating constant,
    • a character constant, or
    • a string literal. There are also enumeration constants as well.

Integer literals

  • An integer literal can be a

decimal, octal, or hexadecimal constant.

  • A prefix specifies the base or radix:
  • A prefix specifies the base or radix:
    • 0x or 0X for hexadecimal,
    • 0 for octal, and
    • nothing for decimal.
  • Suffix with the combination of U and L, for unsigned and long, respectively.
  • The suffix can be uppercase or lowercase and can be in any order.

Here are some examples of integer literals:

512         /* Legal */
265u        /* Legal */
0xFddL      /* Legal */
0012         /* Illegal: Two 0 in the front its an octal declaration */ it should be 012
032UL       /* Illegal: cannot repeat a suffix and it can be 032U */
click below button to copy the code. By - golang tutorial - team

Following are other examples of various type of Integer literals:

24         /* decimal */
0313       /* octal */
0x1b       /* hexadecimal */
60         /* int */
90u        /* unsigned int */
90l        /* long */
90ul       /* unsigned long */
click below button to copy the code. By - golang tutorial - team

Floating-point literals

  • A floating-point literal has an integer part, a decimal point, a fractional part, and an exponent part. You can represent floating point literals either in decimal form or exponential form.
  • While representing using decimal form, you must include the decimal point, the exponent, or both and while representing using exponential form, you must include the integer part, the fractional part, or both. The signed exponent is introduced by e or E.
golang , gopro , google go , golang tutorial , google language , go language , go programming language

Here are some examples of floating-point literals:

3.14159       /* Legal */
314159E-5L    /* Legal */
510E          /* Illegal: incomplete exponent */
210f          /* Illegal: no decimal or exponent */
click below button to copy the code. By - golang tutorial - team
golang , gopro , google go , golang tutorial , google language , go language , go programming language

Sample program

package main

import "fmt"
import "math"

const s string = "constant"

func main() {
fmt.Println(s)

const n = 500000000

const d = 3e20 / n
fmt.Println(d)

fmt.Println(int64(d))

fmt.Println(math.Sin(n))
}
click below button to copy the code. By - golang tutorial - team
golang , gopro , google go , golang tutorial , google language , go language , go programming language

Output

$ go run constant.go 
constant
6e+11
600000000000

Escape sequence

  • Below is the list of escape sequence in Go Programming language.
Escape sequence Meaning
\\ \ character
\' 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

Go program for escape sequence:

package main

import "fmt"

func main() {
   fmt.Printf("Hello\twikitechy!")
}
click below button to copy the code. By - golang tutorial - team

Output for golang program :

Hello   wikitechy!

String literals

String literals or constants are enclosed in double quotes "". A string contains characters that are similar to character literals:

  • Below is the list of escape sequence in Go Programming language.
    • plain characters,
    • escape sequences, and
    • universal characters.
  • You can break a long line into multiple lines using string literals and separating them using whitespaces.
  • Below is an example, all three are identical and provide the same output.
"hello, wikitechy"

"hello, \

wikitechy "

"hello, " "wi" "kitechy "

The const Keyword

  • Constant keyword provides you the option of not modifying the data,
const variablename variabletype = value;
click below button to copy the code. By - golang tutorial - team

sample go program

package main

import "fmt"

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

   area = LENGTH * WIDTH
   fmt.Printf("area : %d", area)   
}
click below button to copy the code. By - golang tutorial - team

Output for the above go program :

area: 20
golang , gopro , google go , golang tutorial , google language , go language , go programming language

Key points on constants

  • const declares a constant value.
  • A const statement can appear anywhere a var statement can.
  • Constant expressions perform arithmetic with arbitrary precision.
  • A numeric constant has no type until it’s given one, such as by an explicit cast.
  • A number can be given a type by using it in a context that requires one, such as a variable assignment or function call. For example, here math.Sin expects a float64.

Related Searches to Floating-point literals