Basic data types for the Go language

Source: Internet
Author: User
Tags arithmetic operators bitwise constant math integer division square root

Basic data types for the Go language 0) Variable declaration

var variable name type = Expression


var int Ten

One of the two parts of "type" or "= expression" can be omitted.

    • 1) Derivation of type information based on initialization expressions
    • 2) The default value is initialized to 0.


var int  // var num int = 0 var Ten // var num int = Ten

1) Integer 1.1) integer type
type name There are no symbols bit number
int8 Yes 8
Int16 Yes 16
Int32 Yes 32
Int64 Yes 64
Uint8 No 8
UInt16 No 16
UInt32 No 32
UInt64 No 64
Int Yes equals the number of CPU bits
UInt No equals the number of CPU bits
Rune Yes Equivalent to Int32
Byte No Equivalent to Uint8
UIntPtr No -

The rune type is a Unicode character type that is equivalent to the Int32 type and is typically used to represent a Unicode code point. Rune and Int32 can be used interchangeably.

byte is an equivalent type of the uint8 type, and the byte type is typically used to emphasize that the value is an original data instead of a small integer.

UIntPtr is an unsigned integer type that does not specify a specific bit size but is sufficient to hold the pointer. The UIntPtr type is only required for the underlying programming, especially where the go language interacts with the C-language function library or the operating system interface.

Regardless of their specific size, int, uint, and uintptr are different types of sibling types. where int and int32 are different types, even if the size of int is 32bit, an explicit type conversion operation is required where int is required as a int32 type, and vice versa.

Signed integers are represented by a 2 complement, which means that the highest bit bit is used to represent the sign bit, and the value of the signed number of an n bit is from -2^{n-1} to 2^{n-1}−1. For example, the range of int8 type integers is from 128 to 127, while the value of uint8 type integers ranges from 0 to 255.

1.2) Integral type operation

Binary operators: Arithmetic operations, logical operations, and comparison operations, operator precedence descending order from top to bottom

*      /     %     <<     >>     &     &^  +      -     |     ^       = = =    <     <=     >      >= && | |

At the same priority, use the left precedence binding rule, but use parentheses to clarify precedence.

Arithmetic operators + 、-、 * and/can be applied to integers, floating-point numbers, and complex numbers, but modulo operator% is used only for operations between integers. The symbol of the% modulo operator is always the same as the symbol of the modulus. The behavior of the division operator depends on whether the operands are all integers, such as the result of 5.0/4.0 is 1.25, but the result of 5/4 is 1 because integer division truncates the remainder in 0 directions.

Two identical integer types can be compared using the following two-tuple comparison operator; The result of a comparison expression is a Boolean type.

    = ==<     <=    >>=     greater than        or equal to

The result of an arithmetic operation, whether unsigned or unsigned, the bit portion of the upper bit that is exceeded is discarded. If the original value is a signed type, and the leftmost bit is 1, the end result may be negative.

Basic types, such as booleans, numeric types, and strings, are comparable, meaning that two values of the same type can be compared by = = and! =.

unary addition and subtraction operators:

+      -      negative

Bit-bit manipulation operators:

symbols Operation whether the operand distinguishes between symbols
& Bitwise arithmetic AND No
| Bitwise Operation OR No
^ Bitwise Operation XOR No
&^ Bit emptying (and not) No
<< Move left Yes
>> Move right Yes

Note The bitwise operation operator ^ is bitwise XOR (XOR) when used as a two-tuple operator, which is a bitwise-inverse when it acts as a unary operator.

The bitwise manipulation operator &^ is used to press position 0 (and not): for the expression z = x &^ y, if a bit bit in Y corresponds to 0, the corresponding bit bit of the result z is equal to the value of the corresponding bit of x, otherwise z corresponds to the bit bit 0.

The right operand (n) of x << N and x >> n must be an unsigned number.

Operation meaning --
<< Move left Left-shift operation fills the right vacant bit with 0
>> Move right The right shift operation of unsigned numbers fills the left vacant bit with 0, and the right shift of the signed number fills the left vacant bit with the value of the sign bit

In general, an explicit conversion is required to convert a value from one type to another type, and the two-dollar operation of arithmetic and logical operations must be of the same type. While this occasionally results in a lengthy expression, it eliminates all types-related problems and makes the program easy to understand.

The conversions between many shaping numbers do not change values; they just tell the compiler how to interpret the value. However, for converting a large integer type to a small integer type, or converting a floating-point number to an integer, you may change the value or lose precision. The conversion of a floating-point number to an integer loses any fractional parts and then truncates to the axis 0 direction.

Integer literals of any size can be written in octal format starting at 0, such as 0666, or in hexadecimal format, starting with 0x or 0X, for example, 0xdeadbeef. hexadecimal digits can be in uppercase or lowercase letters.

1.3) floating point number

The go language provides two precision floating-point numbers, float32 and float64. Their arithmetic specifications are defined by the IEEE754 floating point International standard, and the floating-point specification is supported by all modern CPUs.

These floating-point number types can range from very small to very large. The range limit values for floating-point numbers can be found in the math package. Constant Math. MaxFloat32 represents the maximum value that float32 can represent, about 3.4e38, and the corresponding math. The MaxFloat64 constant is approximately 1.8e308. The minimum values they can represent are approximately 1.4e-45 and 4.9e-324 respectively.

A floating-point number of type float32 can provide a precision of approximately 6 decimal digits, while float64 can provide approximately 15 decimal digits of precision.

function Math. isNaN is used to test whether a number is a non-number nan,math. Nan returns the value corresponding to the non-number. Although you can use math. Nan to represent an illegal result, but testing whether a result is a non-number nan is fraught with risk, because Nan and any number are unequal. If a function returns a floating-point number that may fail, the best practice is to report the failure with a separate flag.

Nan: =//  "false false"
1.4) plural

The go language provides two precision complex types: complex64 and complex128, respectively, corresponding to float32 and float64 two floating point precision. The built-in complex function is used to construct complex numbers, and built-in real and IMAG functions return the real and imaginary parts of the complex numbers, respectively.

Z: = x +== Imag (z)

A complex number can also be compared in equal numbers with = = and! =. They are equal only when the real and imaginary parts of the two complex numbers are equal. The MATH/CMPLX package provides many functions for complex processing, such as finding the square root function of a complex number and the exponentiation function.

1.5) Boolean type

There are only two values for a Boolean type: True and false. The conditional portions of the IF and for statements are boolean-type values, and the comparison operations such as = = and < also produce Boolean values. Unary operator! Corresponds to a logical non-operation, so the value of!true is false.

Boolean values can be used with && (and) and | | (OR) operator, and there may be a short-circuit behavior: If the left-hand value of the operator is already able to determine the value of the entire Boolean expression, the value to the right of the operator will not be evaluated

Boolean values are not implicitly converted to numeric values 0 or 1, and vice versa. You must use an explicit if statement-assisted conversion.

1.6) string

A string is a sequence of bytes that cannot be changed . A string can contain arbitrary data, including a byte value of 0, but is typically used to contain human-readable text. Text strings are often interpreted as UTF8 encoded Unicode code point (rune) sequences.

The built-in len function returns the number of bytes in a string (not the number of Rune characters), the index operation S[i] returns the byte value of byte I, and I must satisfy the 0 <= i < len (s) condition constraints. Attempting to access a byte beyond the range of the string index will result in a panic exception.

The I-byte is not necessarily the I-character of the string, because UTF8 encoding for non-ASCII characters takes two or more bytes.

The substring operation S[i:j] generates a new string based on the first byte of the original s string, beginning to the first J byte (not including J itself). The resulting new string will contain j-i bytes. Both I and J can be ignored, and when they are ignored, 0 is used as the starting position, and Len (s) is used as the end position.

Where the + operator constructs a new string of two string links.

Strings can be compared with = = and <, and comparisons are done by byte-by-bit comparisons, so the result of comparison is the order in which strings are naturally encoded.

String values can also be written in string literals , as long as a series of byte sequences are enclosed in double quotes.

The value of a string is immutable: A string contains a sequence of bytes that is never changed, and of course we can assign a new string value to a string variable.

" Left foot " T:=", right foot"

This does not cause the original string value to be changed, but the variable s will hold a new string value because the + = statement, but T still contains the original string value.

In a double-quoted string literal, you can insert arbitrary data into an escape sequence that begins with a backslash \.

symbols meaning
\a Bell
\b Backspace
\f Page change
\ n Line break
\ r Enter
\ t Tabs
\v Vertical tab
\‘ Single quotation marks (only in the Rune symbol value in the form of ' \ ')
\" Double quotation marks (only for "..." In the form of string literals)
\\ Back slash

You can include any byte in the string value by hexadecimal or octal escape. A hexadecimal escape form is \xhh, where two h represents a hexadecimal number (uppercase or lowercase). An octal escape form is \ooo, which contains three octal o digits (0 to 7), but cannot exceed \377 (\377 is decimal 255). Each single byte is expressed as a specific value.

Native string

A native string literal is ..., using the inverted quotation mark instead of the double quotation mark. In native string literals, there is no escape operation; all content is literal meaning, including backspace and newline, so the native string value in a program may span multiple lines. The only special handling is to delete the carriage return to ensure that the values on all platforms are the same.


Unicode ( collects all the symbology in the world, including accents and other diacritics, tabs and carriage returns, and a number of mysterious symbols, each of which is assigned a unique Unicode code point, The Unicode code point corresponds to the rune integer type in the Go language. The universal data type that represents a Unicode code point is Int32, which is the rune corresponding type in the go language; its synonym Rune Rune is exactly what it means.


UTF8 is a variable-length encoding that encodes a Unicode code point into a sequence of bytes. The UTF8 encoding uses 1 to 4 bytes to represent each Unicode code point, the ASCII partial character uses only 1 bytes, and the characters commonly used part is represented by 2 or 3 bytes. The high-end bit bit of the first byte after each symbol encoding is used to indicate how many bytes are encoded in total. If the high-end bit of the first byte is 0, it represents ASCII characters corresponding to 7bit, and each character of the ASCII character remains one byte, compatible with the traditional ASCII encoding. If the high-end bit of the first byte is 110, then 2 bytes are required; each subsequent high-end bit begins with 10. A larger Unicode code point is also handled with a similar strategy.

0xxxxxxx                             0-127    (ASCII) 110xxxxx 10xxxxxx                    -2047       ( Values <  unused) 1110xxxx 10xxxxxx 10xxxxxx           2048-65535     ( Values <2048  unused) 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx  65536-  0X10FFFF (other values unused)

The Go language source file is UTF8 encoded, and the Go Language processing UTF8 encoded text is also excellent. The Unicode package provides a number of functions that deal with rune character-related functions (such as distinguishing between letters and arrays, or uppercase and lowercase conversions of letters), and the Unicode/utf8 package provides the UTF8 encoding and decoding functions for rune character sequences.

The Unicode escape character in the Go language string value allows us to enter special characters through Unicode code points. There are two forms: the \uhhhh corresponds to the 16bit code point value, \uhhhhhhhh corresponds to the 32bit code point value, where h is a hexadecimal number. UTF8 encoding for each corresponding code point.

For example: The following letter string value represents the same values:

" World " " \xe4\xb8\x96\xe7\x95\x8c " " \u4e16\u754c " " \u00004e16\u0000754c "

Unicode escapes can also be used in Rune characters. The following three characters are equivalent: ' World ' ' \u4e16 ' \u00004e16 '

The value of less than 256 yards can be written in a hexadecimal escape byte, such as ' \x41 ' corresponding to the character ' A ', but for larger code points you must use the \u or \u escape form. As a result, ' \xe4\xb8\x96 ' is not a valid rune character, although these three bytes correspond to an effective UTF8 encoded code point.

Thanks to the excellent design of the UTF8 code, many string operations do not require decoding operations. We can test whether a string is prefixed with another string, is a suffix, or contains a substring test, without decoding it directly. The processing of text after UTF8 encoding is the same as the original byte-processing logic.

Each of the UTF8 characters is decoded, regardless of whether it is explicitly called UTF8. Decoderuneinstring decoding or implicitly decoding in the range loop, if an incorrect UTF8 encoded input is encountered, a special Unicode character ' \ufffd ' is generated, which in print is usually a black hexagonal or diamond shape, It contains a white question mark (?).

String and byte slices

Four packages in the standard library are especially important for string handling: bytes, strings, StrConv, and Unicode packages.

The Strings package provides many functions such as querying, replacing, comparing, truncating, splitting, and merging strings.

The bytes package also provides a number of functions similar to the function, but for the []byte type] that has the same structure as the string. Because character strings are read-only , building a string incrementally causes a lot of allocation and replication. In this case, use bytes. The Buffer type will be more efficient.

The StrConv package provides the conversion of Boolean, Integer, floating-point, and corresponding strings, as well as double-quote-escaping-related conversions.

The Unicode package provides similar features such as IsDigit, Isletter, Isupper, and Islower.

Each function has a single parameter of type Rune, and then returns a Boolean value. Conversion functions such as ToUpper and ToLower are used to convert the case of Rune characters. All of these functions are categorized as letters, numbers, and so on, as defined by the Unicode standard. Strings packages also have similar functions, which are toupper and ToLower, which convert each character of the original string accordingly, and then return the new string.

A string is a read-only byte array that is contained, and is immutable once created. In contrast, a byte-slice element can be modified freely.

The string and byte slice can be converted to each other:

" ABC " B:= []byte(s) S2:string(b)

A string (b) operation that transfers a byte slice to a string is to construct a copy of the string to ensure that the S2 string is read-only.

To avoid unnecessary memory allocations in the conversion, bytes packages and strings also provide a number of utility functions. Here are the six functions in the strings package:

string BOOL  stringintstring) []stringstring  BOOLstringint  func Join (A []stringstring) Strings

The bytes package also corresponds to six functions:

Func Contains (b, Subslice []bytebool  func Count (S, Sep []byteint  func Fields (s []byte) []byte  func hasprefix (S, prefix []bytebool func Index (S, Sep []byteint  func Join (S [] []byte, Sep []  byte) []byte

The only difference between them is that the string type parameter is replaced with the byte slice type parameter.

The bytes package also provides a buffer type for the cache of byte slice. A buffer is initially empty, but as a string, byte, or []byte], the write of data of type can grow dynamically, a bytes. The buffer variable does not need to be initialized because the 0 value is also valid.

When to bytes. Buffer to add UTF8 encoding of any character, it is best to use bytes. The Writerune method of buffer, but the WriteByte method is more efficient for writing ASCII characters like ' [' and '] '.

Conversion of strings and numbers

In addition to the conversion between strings, characters, and bytes, the conversion between strings and numeric values is more common. This type of conversion is provided by the StrConv package.

Several common functions:

Name of function function
StrConv. Itoa () Integer to ASCII
StrConv. Formatint () Format numbers with different binary formats
StrConv. Formatuint () Format numbers with different binary formats
StrConv. Atoi () Resolves a string to an integer
StrConv. parseint () Resolves a string to an integer

Note :

The third parameter of the parseint function is used to specify the size of the integer number; For example, 16 means that int16,0 represents int. In any case, the returned result y is always the int64 type, and you can convert it to a smaller integer type by forcing the type conversion.

FMT is sometimes used as well. SCANF is used to parse input strings and numbers, especially when strings and numbers are mixed in one line, and it can handle incomplete or irregular input flexibly.

1.7) Constants

The value of a constant expression is calculated at compile time, not at run time. The potential type of each constant is the underlying type: Boolean, String, or number. A constant declaration statement defines the name of a constant, similar to the declaration syntax of a variable, and the value of a constant cannot be modified, which prevents accidental or malicious modification at run time.

As with variable declarations, you can declare multiple constants in bulk, which is more appropriate for declaring a set of related constants:

Const (        e   2.71828182845904523536028747135266249775724709369995957496696763         3.14159265358979323846264338327950288419716939937510582097494459 )

The result of all arithmetic operations, logical operations, and comparison operations between constants is also constant, and the type conversion operation on a constant or the following function call returns constant results: Len, cap, Real, imag, complex, and unsafe. Sizeof.

Because their values are determined at compile time, constants can be part of a constituent type, such as specifying the length of an array type:

Const 4 // ParseIPv4 parses an IPv4 address (D.D.D.D). string ) IP {     var p [Ipv4len]byte     //  ... }

A declaration of a constant can also contain a type and a value, but if the type is not explicitly indicated, the type is inferred from the expression on the right.

Iota Constant Generator

Iota constant generator initialization, which is used to generate a set of constants initialized with similar rules, but does not write the initialization expression once per line. In a const declaration statement, in the row of the first declared constant, Iota will be set to 0 and then add one to each row with a constant declaration.

int Const (         = Iota         Monday         Tuesday         Wednesday         Thursday         Friday         Saturday       ) 

Or the following example:

UINT Const (         1//  is up         flagbroadcast            //  supports broadcast Access capability         flagloopback             // is a loopback interface         Flagpointtopoint         //  belongs to a point-to-point link         flagmulticast            //  supports multicast access capability       )

With the increment of iota, each constant corresponds to an expression of 1 << iota, which is a continuous power of 2, corresponding to a bit position respectively.

No type constant

Many constants do not have a definite underlying type. The compiler provides arithmetic operations with a higher precision than the underlying type for numeric constants of these types that do not have a definite underlying type; You can think of at least 256bit of computational precision. There are six types of constants that are not explicitly typed, such as untyped booleans, untyped integers, untyped characters, untyped floating-point numbers, untyped complex numbers, and untyped strings.

By delaying the specific type of the definite constant, the untyped constant can not only provide a higher computational precision, but can be used directly for more expressions without requiring explicit type conversions.

only constants can be untyped. When an untyped constant is assigned to a variable, or if the right-hand expression in the statement contains a value of the explicit type, the untyped constant is implicitly converted to the corresponding type if the conversion is valid.

Whether implicit or explicit conversions, converting one type to another requires the target to represent the original value. For floating-point numbers and complex numbers, rounding processing may occur.

For a variable declaration syntax that does not have an explicit type (including the short variable declaration syntax), an untyped constant is implicitly converted to the default variable type.

Note The default type is regular: An untyped integer constant is converted by default to int, which corresponds to an indeterminate memory size, but floating-point numbers and complex constants are converted by default to Float64 and complex128. The go language itself does not have floating-point and complex types that are not deterministic in memory size, and it is difficult to write the correct numerical algorithm if you do not know the type of floating-point number.

If you want to give a variable a different type, we must explicitly convert the untyped constant to the desired type, or specify a definite type for the declared variable.

Reference: "Go Language Programming" (The Go Programming Language)

Sync posted:Http://

Basic data types for the Go language

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.