Go Language first 2 (memo)

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

look at the go Getting Started guide, here is a more basic, for Go 1.0, here are some of my own feel special and not easy to understand the excerpt of the place!

Last time I said go, the operation between different types must show the conversion, int and uint are not fixed, so int is not int32

    • intAnd on uint 32-bit operating systems, they all use 32 bits (4 bytes), and they all use 64 bits (8 bytes) on 64-bit operating systems.
      Package MainFunc Main () {    var a int    var b int32    a =    B = A + a    //compile error    B = B + 5    //Because 5 is a constant, so you can compile}

Format specifier (should be more than one of these)

In a formatted string, %d used to format integers ( %x and %X numbers used to format the 16 notation) %g for formatting floating-point types ( %f output floating-point numbers, %e output scientific notation notation), %0d An integer that specifies the length of the output, where the beginning of the number 0 is required.

%n.mgUsed to represent the number n and be accurate to the M-bit after the decimal point, in addition to using G, you can also use E or F, for example: Using a formatted string %5.2e to output 3.4 of the result is 3.40e+00 .

%bIs the format identifier used to represent the bit.

4.5.2.3-bit Operation (standby)

Bitwise operations can only be used for variables of integer type, and need to be when they have the same long bit pattern.

%bIs the format identifier used to represent the bit.

Binary operators

    • Bitwise-With & :

      The value at the corresponding position is passed and the result of the operation, see the AND Operator, section 4.5.1, and replace T (true) with 1, replace F (false) with 0

      1 & 1 & 0, 0 & 1
    • Bitwise OR | :

      The value at the corresponding position passes or results of the operation, specifically see or operator, section 4.5.1, and replace T (true) with 1 and F (false) with 0

      1 | 1-11 | 0-10 | 1-10 | 0-0
    • Bitwise XOR ^ :

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

      1 ^ 1 ^ 0 ^ 1 ^ 0 0
    • Bit clear &^ : Sets the value at the specified location to 0.

Unary operators

    • Top-up by position ^ :

      This operator is used with the XOR operator, that is, for unsigned x, use m^x "whole site set to 1" for signed X m=-1 . For example:

      ^2 = ^10 =-01 ^ 10 = 11
    • Bit left shift << :

      • Usage: bitP << n .
      • bitPBits to the left, the blank portion of the right is filled with 0, and if n equals 2, the result is the corresponding multiple of 2, which is the n-th of 2. For example:

        1 << 10//equals 1 KB 1 << 20//equals 1 MB 1 << 30//equals 1 GB
    • Bitwise RIGHT SHIFT >> :

      • Usage: bitP >> n .
      • bitPThe bit to the right moves n bits, the left blank part uses 0 padding, and if n equals 2, the result is the current value divided by 2 of the N-squared.

When you want to assign a result to the first operand, you can either abbreviate a <<= 2 or b ^= a & 0xffffffff .

Bitwise left shifts common use cases for implementing storage units

Constant enumeration of storage units is elegantly implemented using the bitwise left shift with the iota count mate:

Type ByteSize Float64const (    _ = iota///value is ignored by assignment to a blank identifier    KB bytesize = 1<< (10*iota)    MB    GB    tb
  PB    EB    ZB    YB)

Use a bitwise left shift in a communication to indicate a use case for identification

Type Bitflag intconst (    Active bitflag = 1 << iota/1 << 0 = = 1    Send//1 << 1 = 2    Rec Eive//1 << 2 = = 4) Flag: = Active | Send/= = 3

4.5.2.5 arithmetic operators

for integers and floating-point numbers, you can use unary operators ++ (increment) and -- (decrement), but only for suffixes, with ++ and and -- only as statements

Overflow does not produce an error when operating

4.5.4 type aliases

When you use a type, you can give it another name, and then you can use the new name in your code (to simplify the name or resolve the name conflict).

In type TZ int , TZ is the new name of type int (used to represent the time zone in the program), and you can use TZ to manipulate data of type int.

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 type alias gets a new type that is not exactly the same as the original type, and the new type does not have a method that is attached to the original type (chapter 10th); TZ can customize a method to output more user-friendly time zone information.

Practice 4.5 defines a string type alias and Rope declares a variable of that type.

4.5.5 character types

Strictly speaking, this is not a type of Go language, the character is just a special use case for integers. bytethe type is uint8 an alias, and for a traditional ASCII-encoded character that consumes only 1 bytes, there is no problem at all. For example, the var ch byte = 'A' character is enclosed in single quotation marks.

In the ASCII code table, the value of a is 65, and the 16 binary representation is 41, so the following notation is equivalent:

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

( \x always followed by a 16-digit length of 2)

Another possible notation is followed by a \ 3-length decimal number, for example: \377 .

However, Go also supports Unicode (UTF-8), so the characters are also called Unicode code points or runes, and are represented in memory using INT. In the document, the format u+hhhh is generally used, where h represents a 16 binary number. It's actually rune a type of Go, and it's an int32 alias.

When writing Unicode characters, you need to prefix or precede the 16 binary numbers \u \U .

Because Unicode occupies at least 2 bytes, we use int16 or int type to represent it. If you need to use up to 4 bytes, the prefix is \U \u always followed by a 16-digit length of 4, followed by a \U 8-length 16-digit prefix.

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 Beta r41-3b2-101234u+0041-u+03b2-u+101234

The format specifier is %c used to represent a character, or an integer used to represent the character when used in conjunction with a character, and a string with the %v %d %U output format u+hhhh (see section 5th. 4.4 for another example).

The package unicode contains some very useful functions for testing characters (which ch represent characters):

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. utf8the package has more functions related to rune.



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.