GO language fully resolved GO! GO! GO! (A) basic grammar (To be continued)

Foreword

Applicable probably used Golang programming friends, this document belongs to a summary of the leak filled.

 

A: GO data type - the type of foundation

0, define a standard format for the variable: var type variable name

1, digital type

     1) Type Integer: int8, int16, int32, int64 (signed) / uint8, uint16, uint32, uint64 (unsigned), respectively, occupy 8bit, 16bit, 32bit, 64bit;

                    int / uint, take up much space in the CPU must take the machine word size, typically 32bit or 64bit

                    rune, it indicates the storage space occupied by a character

                              Water Devils: specifically a few bytes? Some say the document is 32bit, and int32 equivalent, but I understand that is not fixed, according to the coding rule, the code point using a few bytes rune is a few bytes, to be determined -

                    UIntPtr, unsigned integer type, without specifying how many bit, but to be able to accommodate pointer, generally used for low-level programming, such as C language library functions and the operating system, or when the interaction with this type will

      byte, a type of the equivalent type uint8

     2) Type point values: float32 / float64

     3) complex type: complex64 / complex128

2, Boolean type: bool

3, string type: string, the string is an underlying structure, also relying on the underlying array, slice, and the like, in fact, the string can also be operated by means of slice                                                                  

a. string type substructure 
 type StringHeader struct {
    the Data UIntPtr the Data // is a pointer type, he points to a bottom array of characters, also known as the face value, with a utf-8 encoded string can never be changed the sequence of characters. See coding knowledge https://www.cnblogs.com/shuiguizi/p/11372985.html
    Len int // The length of the string, we use the built-in len function when, in fact, the value read
}

. b string to Slice
  ST: = "Hello, World"
  SL: = S [:. 5]    

There are a few points: 1) copy of the string just copy the structure of the underlying data or the "hello world", which makes the cost of copying the string of any length cheap, programmers can go ahead and splurge

                      2) re-emphasized the value of the string can not be changed, so never expect to get help slice modifications, but who represents the string variable can be changed, such as s1 + = ", you da ma", your uncle (hello world) or that your uncle, your aunt (s1) is not the one that you Aunt

                     By comparing the comparison byte by byte completed, so that the result of the comparison string natural sequence coding; 3) may string <== for comparison and.

In addition, knowledge is a very important point is that there are two string representation, namely: the native representation and interpreted representation.

var str1 String = "STR" // interpreted notation, we need to use double quotation marks "" "wrap character sequence.    
 var str1 String =` str` // native representation, required by reverse quotes "` "the character sequence wrap up,

Water Monster: parsing type, it is our common, named Incredibles, "" to explain the contents of this string is "like this, if there are special characters, etc., in accordance with the actual meaning of special characters to deal with."

           Original type, was often encountered in languages ​​go, meaning that `the content here is what you see looks like, what the escape character is all a soulless dead character only`         

 

4, time and date type

 

5, pointer type: ptr

 

Two: GO data types - Type compound

1, the array type / slice types: array type and not a specific keyword, and C language, can be the basis of any type of array, the array Once initialized, the share will not change storage, usage is as follows:

                             var myArray = [3]int{1,2,3}  或 var myArray = [3]int{1,2,3}              //即是一个int类型的数组
                       切片类型和数组有着很深的渊源,可以认为是可变长的数组,实际上他的存在是依托于底层数组的,用法如下:
              var myslice1 =[]int{1,2,3}    或   var myArray = [5]int{1,2,3,4,5}    var myslice2 = myArray[1:4]    

解析:《圣经》上说:

A data slice is a lightweight structure, provided access to the array sequence (or all) of the functional elements, and the bottom slice does reference to an array object. A slice consists of three parts: the pointer, length and capacity.
The pointer points to the first address element corresponding to a slice of the underlying array element, to be noted that the first element of the first slice is not necessarily element array.
Length corresponds to the number of elements in the slice;
length can not exceed the capacity, the capacity of the slice is generally open from
the end position to the start position of the underlying data. Built-in functions return len and cap length and volume slice.

    The underlying data can be shared among a plurality of slice, and the reference range may overlap portions of the array. Use a diagram to represent is:

                             

 

Water Devils: With this diagram to interpret what the relationship slice and arrays

             First, a slice structure comprising three filed: The first is a pointer, showing the underlying array that relies on this slice, attention only "based on" is not "fully representative", because the pointer from the bottom of the array may be arbitrarily position began to count

                                                                                 The second is the length, also called View, i.e., "Vision", this indicates slice steerable scope of the underlying array is so great

                                                                                 The third is the capacity of a total underlying array so big, the top days your vision does not exceed the entire array, so that you can limit the range of operation, and by operation of the longitudinal extension of its capacity the same:

                                                                      slice1 = slice1[:cap(slice1)]

                       As another example, the above figures, the underlying array is months, a total of 13 elements. Q2 4 start from the position, your current view is three, more than you can handle 9; summer is starting at position 6, the current view is three, can handle up to 7

             Then, not a comparison between Slice; zero value which is nil, showing no underlying array, the length and capacity are 0, for example, [] int {}, or make ([] int, 3 [3: 0])

                        General requirements go function should be treated the same language nil and a length of the slice 0, note that this is coded Compliant

            Finally, modify the elements of slice of that will affect the underlying array, slice replication but also replication of the underlying array slice structure or the array

            Most Finally, the underlying array look like?

        type SliceHeader struct {
          Data uintptr
          Len int
          Cap int
             }

 

 

2, the structure

3,map

 

A: GO data types - Reference types

1, pointer: *, for example:

2. Slice: *, for example:

3, the dictionary: *, for example:

4, function: *, for example:

1, the channel: Channel, for example:

 

Guess you like

Origin www.cnblogs.com/shuiguizi/p/11372635.html
Go