"Go" Go language learning note two

Source: Internet
Author: User

Basic type:

Boolean Type: Boolean

Integral type: INT8,BYTE,INT16,INT,UINT,UINTPTR, etc.

Floating point type: Float32,float64

Plural type: complex64,complex128

String Type: String

Character Type: Rune

Fault Type: Error

Composite type:

Pointer: pointer

Arrays: Array

Slices: Slice

Dictionary: Map

Channel: Chan

struct: struct

Interface: interface

1. Boolean type, same as Boolean in other languages

2. Integral type:

A, type representation

It should be noted that int and int32 are considered to be two different types in the go language, and the compiler will not help you do the type conversion automatically, such as the following example will have a compilation error:
var value2 int32
Value1: =///value1 will be automatically deduced as int type value2 = value1//Compile Error

The compilation error resembles the following:
Cannot use Value1 (type Int.) as type Int32 in assignment. This compilation error can be resolved with coercion type conversion:
Value2 = Int32 (value1)//compilation by of course, developers need to be aware of the loss of data accuracy that occurs when the data length is truncated (for example, when forcing type conversions).
A floating-point number is coerced to an integer) and the value overflows (when the value exceeds the range of values of the converted target type).

B, numerical operations

The go language supports the following general integer operations: + 、?、 *,/, and%. Subtraction is not explained in detail, it is necessary to say that% and in C language is the same as the remainder of the operation, such as:

5%3//Result: 2

C, Comparison operation

The go language supports several comparison operators:>, <, = =, >=, <=, and! =. This is the same as most other languages and is exactly the same as the C language.

  The following is an example of a conditional judgment statement:
I, J: = 1, 2 if i = = J {        FMT. Println ("I and J are equal.")    }

Two different types of integers cannot be directly compared, such as the number of int8 types and the number of int cannot be directly compared, but
All types of integer variables can be directly compared to literal constants (literal), such as:

var i int32var j int64i, j = 1, 2if i==j{//Compilation error FMT. Println ("I and J are equal.")} If i==1| | j==2{//compiled via FMT. Println ("I and J are equal.")}

D, bit arithmetic

Most of the bit operators in the go language are similar to the C language, except that they are ~x in the C language and ^x in the go language.

3, floating-point type:

Float is used to represent data that contains a decimal point, for example, 1.234 is a floating-point data. Floating-point types in the go language are represented by the IEEE-754 standard.
A, floating-point number representation
The go language defines two types of float32 and float64, where float32 is equivalent to the float type of C, and float64 is equivalent to the double type of the C language.
In the go language, the code that defines a floating-point variable is as follows:

var fvalue1 float32fvalue1 = 12fvalue2: = 12.0//If you do not add a decimal point, fvalue2 is deduced as an integer instead of a floating-point type

For the fvalue2 that the type is automatically deduced in the example above, it is important to note that its type is automatically set to float64, regardless of whether the number assigned to it is represented by a 32-bit length. Therefore, for the above example, the following assignment will result in a compilation error:
Fvalue1 = Fvalue2
Instead, you must use such coercion type conversions:
fvalue1 = float32 (fvalue2)

B, floating-point comparison
Because floating-point numbers are not an exact representation, it is not feasible to use = = as an integer to determine whether two floating-point numbers are equal, which can lead to unstable results.
The following is a recommended alternative:



}

4. Plural type:

The complex number is actually composed of two real numbers (represented by floating-point numbers in the computer), one representing the real part (real), and one representing the imaginary part (IMAG). The plural of the go language is easy to understand if you know what the math complex is all about.

An example of a complex number representation is as follows:

var value1 complex64value1 = 3.2 + 12i  //complex type consisting of 2 float32  value2: = 3.2 + 12i   //value2 is complex128 type value3: = Complex (3.2)  //VALUE3 results with value2

b, the real part and the imaginary part
For a complex z = complex (x, y), it is possible to get the real part of the complex by using the go language built-in function real (z), which is X, which obtains the imaginary part of the complex by Imag (Z), that is, Y.
For more functions on complex numbers, consult the documentation for the MATH/CMPLX standard library.

5. String:

The Declaration and initialization of strings in the go language is simple, for example:

var str string//Declare a string variable str = "Hello World"//String Assignment ch: = str[0]//Take the first character of the string fmt. Printf ("The length of \"%s\ "is%d \ n", str, Len (str)) FMT. Printf ("the first character of \"%s\ "is%c.\n", str, CH) outputs the result:    the length of "Hello World" is one and the first    Charac ter of ' Hello world ' is H.

The contents of a string can be obtained in a manner similar to an array subscript, but unlike an array, the contents of the string cannot be modified after initialization, such as the following example:
str: = "Hello World"//string also supports the practice of initializing when declaring

Str[0] = ' X '//Compile Error
The compiler will report an error similar to the following:

Cannot assign to str[0]

A, string manipulation

The usual string operations are shown in table 2-3.

For more string operations, refer to the standard library strings package.

B, String traversal

The go language supports two ways of traversing a string. One is traversing in a byte array:

str: = "Hello, world" N: = Len (str) for I: = 0; I < n; i++ {ch: = Str[i]//based on the subscript to the character in the string, the type is byte        fmt. Println (i, CH)}

The other is to traverse with Unicode characters:

str: = "Hello, world" For I, ch: = Range str {FMT. Println (i, CH)//ch is of type Rune}

When traversing in Unicode characters, the type of each character is Rune (the earlier Go language uses the int type to represent Unicode characters), rather than byte.

6. Character Type:

Supports two character types in the go language, one is byte (which is actually an alias of Uint8), represents the value of a single byte of the UTF-8 string, and the other is Rune, which represents a single Unicode character. For rune related operations, refer to the Unicode package of the Go standard library. In addition the Unicode/utf8 package also offers
The conversion between UTF8 and Unicode. For the sake of simplifying the language, most APIs in the go language assume that the string is UTF-8 encoded. Although Unicode characters are supported in the standard library, they are actually less used.

Not to be continued ...

  

  

  

  

  

  

  

  

"Go" Go language learning note two

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: info-contact@alibabacloud.com 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.