0%

Go_Fundamental_Learning_Build In Type And Literal Value

Build in type

There are 17 build in type in Golong; We can use them without import any package

Boolean (1)

  • bool: defalut value is false

String (1)

  • string: default value is empty string ``;

Numeric (15)

Interger numeric type (11)

  • int8, uint8, int32, uint32, int64, uint64, int, uint, uintprt

Float point type (2)

  • float32, float64

Complex type (2)

  • complex64, complex128

Memory Size

If a value occupies N bits in memory, we say the size of value is N bits. The size of all values of a type is always the same, So the value size means the type size.
Byte size is often used to measure memory, One byte equal to eight bits. So the size the `int32 is four bytes. The picture show the size of different type.

image-20220502155346311

Zeros Value of Build In type

Zeros value is also the default value ;

bool: false
numeric: 0
String: ""

Basic Value Literals

A literal of a value is a text representation of the value in code. A value may have many lliterals. The literals denoting values of basic types are called basic value literals.

Boolean Value Literals

false and true could view as the boolean literals.

Numeric Value Literals

Integer value literals

There are four integer value literal forms, the decimal form, the octal form, the hex form and the binary form. For example the value 15 could represent as follow:

1
2
3
4
5
6
7
8
9
10
11
0xF // the hex form (starts with a "0x" or "0X")
0XF

017 // the octal form (starts with a "0", "0o" or "0O")
0o17
0O17

0b1111 // the binary form (starts with a "0b" or "0B")
0B1111

15 // the decimal form (starts without a "0")

Floating-point value literals

  • Reference : Go 101

Imaginary value literals

  • Reference : Go 101

Tips:

  • Use _ in numeric literal for better readability;

    underscores _ can appear in integer, floating-point and imaginary literals as digit separators to enhance code readability. But please note, in a numeric literal,

    • any _ is not allowed to be used as the first or the last character of the literal,
    • the two sides of any _ must be either literal prefixes (such as 0X) or legal digit characters.
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    // Legal ones:
    6_9 // == 69
    0_33_77_22 // == 0337722
    0x_Bad_Face // == 0xBadFace
    0X_1F_FFP-16 // == 0X1FFFP-16
    0b1011_0111 + 0xA_B.Fp2i

    // Illegal ones:
    _69 // _ can't appear as the first character
    69_ // _ can't appear as the last character
    6__9 // one side of _ is a illegal character
    0_xBadFace // "x" is not a legal octal digit
    1_.5 // "." is not a legal octal digit
    1._5 // "." is not a legal octal digit

**rune value literals **

Rune types are special integer types, so all rune values can be denoted by integer literals. On the ather hand, many values of all kinds of integer could represend by rune literal .

A rune value is intended to store a Unicode code point. Generally, we can view a code point as a Unicode character, but we should know that some Unicode characters are composed of more than one code points each.

A rune literal is expressed as one or more characters enclosed in a pair of quotes. The enclosed characters denote one Unicode code point value. There are some minor variants of the rune literal form. The most popular form of rune literals is just to enclose the characters denoted by rune values between two single quotes. For example

1
2
3
'a' // an English character
'π'
'众' // a Chinese character

The following rune literal variants are equivalent to 'a' (the Unicode value of character a is 97).

1
2
3
4
5
6
// 141 is the octal representation of decimal number 97.
'\141'
// 61 is the hex representation of decimal number 97.
'\x61'
'\u0061'
'\U00000061'

Please note, \ must be followed by exactly three octal digits to represent a byte value, \x must be followed by exactly two hex digits to represent a byte value, \u must be followed by exactly four hex digits to represent a rune value, and \U must be followed by exactly eight hex digits to represent a rune value. Each such octal or hex digit sequence must represent a legal Unicode code point, otherwise, it fails to compile.

The following program will print 7 true texts.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package main

func main() {
println(0o17 == '\x0F')
println(97 == '\x61')
println(0x61 == '\x61')
println('a' == 97)
println('a' == '\141')
println('a' == '\x61')
println('a' == '\u0061')
println('a' == '\U00000061')
println(0x61 == '\x61')
println('\u4f17' == '众')
}

String value literals

String values in Go are UTF-8 encoded. In fact, all Go source files must be UTF-8 encoding compatible.

There are two forms of string value literals, interpreted string literal (double quotes form) and raw string literal (back quotes form). For example, the following two string literals are equivalent:

1
2
3
4
5
6
7
// The interpreted form.
"Hello\nworld!\n\"你好世界\""

// The raw form.
`Hello
world!
"你好世界"`

In the above interpreted string literal, each \n character pair will be escaped as one newline character, and each \" character pair will be escaped as one double quote character. Most of such escape character pairs are the same as the escape character pairs used in rune literals introduced above, except that \" is only legal in interpreted string literals and ``` is only legal in rune literals.

The character sequence of \, \x, \u and \U followed by several octal or hex digits introduced in the last section can also be used in interpreted string literals.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// The following interpreted string literals are equivalent.
"\141\142\143"
"\x61\x62\x63"
"\x61b\x63"
"abc"

// The following interpreted string literals are equivalent.
"\u4f17\xe4\xba\xba"
// The Unicode of 众 is 4f17, which is
// UTF-8 encoded as three bytes: e4 bc 97.
"\xe4\xbc\x97\u4eba"
// The Unicode of 人 is 4eba, which is
// UTF-8 encoded as three bytes: e4 ba ba.
"\xe4\xbc\x97\xe4\xba\xba"
"众人"

Please note that each English character (code point) is represented with one byte, but each Chinese character (code point) is represented with three bytes.

In a raw string literal, no character sequences will be escaped. The back quote character is not allowed to appear in a raw string literal. To get better cross-platform compatibility, carriage return characters (Unicode code point 0x0D) inside raw string literals will be discarded.

Zero values of string types can be denoted as "" or ```` in literal.