Go language beginners 2 (Memo)

Source: Internet
Author: User

ViewGo Getting Started Guide, which is a basic introduction here. For go 1.0, the following is an excerpt from what I think is special and hard to understand!

 

The previous operation in go must display conversions between different types. int and uint are not fixed, so Int Is Not int32.

  • intAnduintOn 32-bit operating systems, both use 32-bit (4 bytes), and on 64-bit operating systems, both use 64-bit (8 bytes ).
    Package mainfunc main () {var A int var B int32 A = 15 B = a + A // compilation error B = B + 5 // because 5 is a constant, so you can compile it}

Format the specifiers (more than that)

In the formatted string,%dUsed to format integers (%xAnd%XFormat a hexadecimal number ),%gUsed to format floating point (%fOutput floating point number,%eOutput scientific counting Notation ),%0dSpecifies an integer with a fixed length. The value 0 at the beginning is required.

%n.mgIt is used to represent the number N and is accurate to the M digit after the decimal point. In addition to G, you can also use E or F. For example, you can use a formatted string.%5.2eTo output 3.4 results:3.40e+00.

%bIs a formatted identifier used to represent a bit.

 

 

4.5.2.3 bit operation (Backup)

Bitwise operations can only be used for integer-type variables, and must be performed when they have an equal-length long-Bit mode.

%bIs a formatted identifier used to represent a bit.

Binary Operators

  • Bitwise AND&:

    The value at the corresponding position and the calculation result. For more information, see section 4.5.1 of the and operator. replace T (true) with 1 and F (false) with 0.

    1 & 1 -> 11 & 0 -> 00 & 1 -> 00 & 0 -> 0
  • By bit or|:

    The value at the corresponding position goes through or the operation result. For more information, see section 4.5.1 of the OR operator. replace T (true) with 1 and F (false) with 0.

    1 | 1 -> 11 | 0 -> 10 | 1 -> 10 | 0 -> 0
  • Bitwise OR^:

    The values at the corresponding position are combined according to the following rules:

    1 ^ 1 -> 01 ^ 0 -> 10 ^ 1 -> 10 ^ 0 -> 0
  • Bit clearing&^: Set the value at the specified position to 0.

Unary operator

  • Complement by bit^:

    This operator is used together with an exclusive or operator, that ism^xFor unsigned X, use "set all bits to 1". For Signed X, usem=-1. For example:

    ^2 = ^10 = -01 ^ 10 = -11
  • Shift left<<:

    • Usage:bitP << n.
    • bitPTo the left, and the left blank part is filled with 0. If n is equal to 2, the result is a multiple of 2, that is, the N power of 2. For example:

      1 <10 // equal to 1 kb 1 <20 // equal to 1 MB 1 <30 // equal to 1 GB
  • Shift right>>:

    • Usage:bitP >> n.
    • bitPTo the right, and the left blank part is filled with 0. If n is equal to 2, the result is that the current value is divided by the N power of 2.

If you want to assign the result to the first operand, it can be abbreviateda <<= 2Orb ^= a & 0xffffffff.

Common Use Cases for implementing storage unit shift with bits left

The constant enumeration of the storage unit can be elegantly realized using the bitwise left shift and iota count combination:

Type bytesize float64const (_ = iota // The null identifier is assigned to ignore the value kb bytesize = 1 <(10 * iota) mb gb tb Pb eb zb Yb)

Use Case with shift left in Communication

type BitFlag intconst (    Active BitFlag = 1 << iota // 1 << 0 == 1    Send // 1 << 1 == 2    Receive // 1 << 2 == 4)flag := Active | Send // == 3

4.5.2.5 Arithmetic Operators

You can use the unary operator for integers and floating-point numbers.++(Incremental) and--(Decrease), but can only be used for suffixes,++And--Can only be used as a statement

Overflow During computation will not produce errors

 

 

4.5.4 type alias

 

When you are using a type, you can give it another name, and then you can use a new name in your code (for simplifying the name or resolving name conflicts ).

 

Intype TZ intIn, tz Is the new name of the int type (used to represent the time zone in the Program), and then you can use TZ to operate the int type data.

 

Example 4.11 type. Go

 

Package mainimport "FMT" type TZ intfunc main () {var A, B tz = 3, 4 C: = a + B FMT. printf ("C has the value: % d", c) // output: C has the value: 7}

 

In fact, the new type obtained by the Type alias is not exactly the same as the original type, and the new type does not have the method attached to the original type (Chapter 10th ); TZ can customize a method to output more user-friendly time zone information.

 

Practice 4.5DefinestringType aliasRopeAnd declare a variable of this type.

 

4.5.5 character type

Strictly speaking, this is not a type of the Go language. The character is only a special example of an integer.byteType isuint8For traditional ASCII characters that only occupy 1 byte, there is no problem at all. For example:var ch byte = ‘A‘The characters are enclosed in single quotes.

In the ASCII code table, the value of a is 65, and the hexadecimal representation is 41, so the following statement is equivalent:

VaR ch byte = 65 or var ch byte = '\ x41'

(\xAlways follow the hexadecimal number with a length of 2)

Another possible method is\Followed by a decimal number of 3, for example:\377.

But go also supports Unicode (UTF-8), so the characters are also called Unicode code points or Runes and are represented in memory using Int. Generally, the format U + hhhh is used in this document. h indicates a hexadecimal number. ActuallyruneIt is also a type in go, and isint32.

When writing Unicode characters, you must add a prefix before the hexadecimal number.\uOr\U.

Because Unicode occupies at least 2 bytes, we useint16OrintType. If you want to use 4 bytes\UPrefix\uThe prefix is always followed by the hexadecimal number with a length of 4.\UFollowed by the hexadecimal number of 8 in length.

Example 4.12 Char. Go

var ch int = ‘\u0041’var ch2 int = ‘\u03B2’var ch3 int = ‘\U00101234’fmt.Printf(“%d - %d - %d\n”, ch, ch2, ch3) // integerfmt.Printf(“%c - %c - %c\n”, ch, ch2, ch3) // characterfmt.Printf(“%X - %X - %X\n”, ch, ch2, ch3) // UTF-8 bytesfmt.Printf(“%U - %U - %U”, ch, ch2, ch3) // UTF-8 code point

Output:

65 - 946 - 1053236A - β - r41 - 3B2 - 101234U+0041 - U+03B2 - U+101234

Format specifiers%cUsed to indicate characters. When used together with characters,%vOr%dReturns the integer that represents the character;%UOutput string in the format of U + hhhh (For another example, see section 5.4.4 ).

PackageunicodeContains some very useful functions for test characters (wherechCharacters ):

Determines whether it is a letter: Unicode. isletter (CH) determines whether it is a number: Unicode. isdigit (CH) determines whether it is a blank Symbol: Unicode. isspace (CH)

These functions return a Boolean value. Packageutf8More Rune-related functions are available.

 



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.