# Golang Tutorial Part #7 ~ Constants

Hello, how are you all, friends, back again with Teman Ngoding. Previously we have studied Data Types in Golang, this time we will discuss constants in Golang.

Constants refer to fixed values that cannot be changed by the program during execution. These fixed values are also called literals.

Constants can be any of the basic data types such as integer constants, floating constants, character constants, or string literals. There are also count constants as well.

Constants are treated like ordinary variables except that their value cannot be changed after their definition.

Data such as pi (22/7), the speed of light (299,792,458 m/s), are good examples of data if they are declared as constants rather than variables, because their values are fixed and do not change.

# Literal Integer

Integer literals can be decimal, octal, or hexadecimal constants. The prefix specifies the base or radix: 0x or 0X for hexadecimal, 0 for octal, and nothing for decimal. Integer literals can also have a suffix that is a combination of U and L, for unsigned and long, respectively. The suffix can be upper or lower case and can be in any order.

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 */`

Here is another example of different types of Integer literals:

`85         /* decimal */0213       /* octal */0x4b       /* hexadecimal */30         /* int */30u        /* unsigned int */30l        /* long */30ul       /* unsigned long */`

# Literal floating point

A floating-point literal has an integer part, a decimal point, a fractional part, and an exponential part. You can represent floating point literals in either decimal or exponential form.

When representing using the decimal form, you must enter the decimal point, the exponential, or both and when representing using the exponential form, you must include the integer part, the fractional part, or both. The signed exponent is introduced by e or E.

Here are some examples of floating point literals:

`3.14159       /* Legal */314159E-5L    /* Legal */510E          /* Illegal: incomplete exponent */210f          /* Illegal: no decimal or exponent */.e55          /* Illegal: missing integer or fraction */`

# Escape Sequence

When certain characters are preceded by a backslash, that character will have a special meaning in Go. This is known as Escape Sequence code which is used to represent newline (\n), tab (\t), backspace, etc. Here, you have a list of some of those escape sequence codes.

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

Output

`Hello World!`

# String Literals in Go

String literals or constants are enclosed in double quotes “”. Strings contain characters similar to character literals: plain characters, escape sequences, and universal characters. You can break a long line into multiple lines using string literals and separate them using whitespace.

Here are some examples of string literals. These three forms are identical strings:

`"hello, dear""hello, \dear""hello, " "d" "ear"`

const keyword

You can use the const prefix to declare constants of a specific type as follows:

`const variable type = value;`

Contoh berikut menunjukkan cara menggunakan kata kunci const:

So this tutorial I can convey, don’t forget to also read the previous tutorial.

Golang Tutorial Part #6 ~ Data Types

Golang Tutorial Part #5 ~ Golang Variables

Golang Tutorial Part #4 ~ First Program : Hello Word

# Thank you.

--

--

--

## More from Teman Ngoding

Love podcasts or audiobooks? Learn on the go with our new app.