[GO] variable type and definition

GO: Microsoft's VC compiler fills the uninitialized stack space with hexadecimal 0xCC, while the uninitialized heap space is filled with 0xCD, and 0xCCCC and 0xCDCD correspond to the words "hot" and "Tun" in Chinese GB2312 code

catalogue

Basic type

GO pointer

String length

Basic type

string
int,int8,int16,int32,int64
uint,uint8,uint16,uint32,uint64,uintptr
 Byte / / alias of uint8
 The alias of rune // int32 represents a Unicode code
float32,float64
complex64,complex128
​
When a variable is declared, the system automatically gives it a zero value of this type: int is 0, float is 0.0, bool is false, string is an empty string, pointer is nil, etc. All memory is initialized in Go,

Like the var declaration statement, the short variable declaration statement can also be used to declare and initialize a set of variables:

i, j := 0, 1

Standard format for variable initialization

var variable name type = expression: var hp int = 100
                 Omitted: var hp = 100

Note: in multiple short variable declarations and assignments, at least one newly declared variable appears in the lvalue. Even if other variable names may be declared repeatedly, the compiler will not report an error. The code is as follows:

conn, err := net.Dial("tcp", "127.0.0.1:8080")
conn2, err := net.Dial("tcp", "127.0.0.1:8080")

Character types: byte and run

There are two characters in Go language:

  • One is uint8 type, or byte type, which represents a character of ASCII code.
  • The other is the rune type, which represents a UTF-8 character. When you need to process Chinese, Japanese or other composite characters, you need to use the rune type. The run type is equivalent to the int32 type.

 —— Byte type is the alias of uint8. For traditional ASCII coded 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 writing method is equivalent:

var ch byte = 'A'
var ch byte = 65  
var ch byte = '\x41'      //(\ x is always followed by a hexadecimal number of length 2)

 —— The Go language also supports Unicode (UTF-8), so characters are also called Unicode code points or runes, and are represented by ints in memory. In documents, the format U+hhhh is generally used, where h represents a hexadecimal number. When writing Unicode characters, you need to prefix the hexadecimal number with \ u or \ U. Because Unicode takes at least 2 bytes, we use int16 or int type. If you need to use up to 4 bytes, use the \ u prefix. If you need to use up to 8 bytes, use the \ u prefix.

var ch int = '\u0041'
var ch2 int = '\u03B2'
var ch3 int = '\U00101234'
fmt.Printf("%d - %d - %d\n", ch, ch2, ch3) // integer
// Output: 65 - 946 - 1053236 [hex to decimal (% d)]

fmt.Printf("%c - %c - %c\n", ch, ch2, ch3) // character
// Output: A- β -  r [character (% c)]

fmt.Printf("%X - %X - %X\n", ch, ch2, ch3) // UTF-8 bytes
// Output: 41 - 3B2 - 101234 [hex (% X)]

fmt.Printf("%U - %U - %U", ch, ch2, ch3)   // UTF-8 code point
// Output: U + 0041 - U + 03b2 - U + 101234 [U + X (% U)]

What is the difference between UTF-8 and Unicode?

Unicode, like ASCII, is a character set.

The character set assigns a unique ID to each character. All characters we use have a unique ID in the Unicode character set. For example, the encoding of a in Unicode and ASCII in the above example is 97. The code of the Chinese character "you" in Unicode is 20320, and the corresponding ID of the character will be different in the character sets of different countries. In any case, the ID of characters in Unicode will not change.

UTF-8 is an encoding rule that encodes the ID of characters in Unicode in some way. UTF-8 is a variable length encoding rule, ranging from 1 to 4 bytes. The coding rules are as follows:

  • 0xxxxxx represents text symbols 0 ~ 127, which is compatible with ASCII character set.
  • From 128 to 0x10ffff indicates other characters.


According to this rule, the character coding of Latin language family generally occupies one byte per character, while Chinese occupies three bytes per character.

Unicode in a broad sense refers to a standard, which defines the character set and coding rules, that is, Unicode character set and UTF-8, UTF-16 coding, etc.

GO pointer

1. Both structure variable and structure pointer can access member (Type.pam) with subscript, and Go will automatically identify its type

func (p *Ptr_test)modify_number_1()  //When called, it will affect the state of the variable itself
func (p Ptr_test)modify_number_2()   //Calling does not affect the state of the variable itself    

2. Function parameter transfer is value transfer, that is, the parameters in the function are copies of the transferred parameters

func (p *Ptr_test)modify_ptr(ph *Ptr_test){
      p  = ph   //Does not change the value of the object to which p refers
      *p = *ph  //Will change the value of the object indicated by P
}

3. The difference between make and new: make is used to create slices (variable arrays), map s and channels (queues). It refers to a memory that has been initialized and can be used directly; However, new creates a pointer to the corresponding type. The memory pointed to by the pointer can only be used after initialization. (make is a reference and new is a pointer. If you need to create a pointer with make, you can directly set the type as a pointer)

 

-make

slice := make([]Ptr_test, 1) // At this time, slice[0] can be accessed, and its members are all initial values. The subsequent use of append will start from 1
slice = append(slice,pam_1)
​
hash  := make(map[int]Ptr_test, 10)
hash["kaka"] = pam_1
​
​
ch    := make(chan Ptr_test, 5)  // The capacity is 5 (must be specified), overflow exceeds 5, and the length is the actual stored variable size of the current channel
ch2   := make(chan interface{}, 5)  //Specify any type
​
ch <- pam1
ch <- pam2 // Put in channel
​
pam3 := <- ch  // ch first in first out principle. At this time, pam3 is the copy of pam1
<- ch         // After execution, ch is empty and its length becomes 0 

——New: you rarely need to use new unless you really like the word.

// section
slice_a  := new([]Ptr_test) // Available after initialization
*slice_a  = append(*slice_a, a_pam1) //Initialization, (* slice_a)[0].name access member
​
//Hashtable 
hash_a := new(map[string]Ptr_test)
//map needs to be initialized before it can be used
*hash_a = map[string]Ptr_test{}        //Type 1
*hash_a = make(map[string]Ptr_test, 0) //Type 2
(*hash_a)["kaka"] = a_pam1
fmt.Printf("hash: %s ",(*hash_a)["kaka"].name)

character string

1 - string length

  • Ascii string length uses the len() function to represent the number of ASCII characters or byte length of the string.
  • Unicode string length uses the utf8.RuneCountInString() function.
str_name := "Kaka kaka"
n1 := len(str_name)  //The length is 11, go using UTF-8 encoding, 3 bytes for one Chinese.
n2 := utf8.RuneCountInString(str_name) // Length 6

2-string traversal

  • ASCII string traversal uses subscripts directly.
  • for range is used for Unicode string traversal, and character by character traversal will appear
theme := "Sniper start"
for _, s := range theme {
    fmt.Printf("Unicode: %c  %d\n", s, s)
}

Unicode: Sniper   twenty-nine thousand four hundred and one
Unicode: hit   twenty thousand nine hundred and eighty-seven
Unicode:    32
Unicode: s  115
Unicode: t  116
Unicode: a  97
Unicode: r  114
Unicode: t  116

3-   String interception

tracer := "Final Destination, death bye bye"
comma := strings.Index(tracer, ", ")
pos := strings.Index(tracer[comma:], "death")
fmt.Println(comma, pos, tracer[comma+pos:])

// strings.Index: forward search substring.
// strings.LastIndex: reverse search for substrings.
// The starting position of the search can be made by slice offset.

       ——  The comma position is 12, while POS is the relative position, with a value of 3. In order to obtain the position of the second "God of death", that is, the string after the comma, we must add the relative offset of POS to comma to calculate the offset of 15, and then calculate the final substring through slice tracer[comma+pos:] to obtain the final result: "God of death bye bye".

4 - modify string

angel := "Heros never die"
angleBytes := []byte(angel)  // In line 3, convert the string to an array of strings. byte = uint8

for i := 5; i <= 10; i++ {
    angleBytes[i] = ' '      // Replace the word never with a space.
}
fmt.Println(string(angleBytes)) // Heros      die
  • The string of Go language is immutable.
  • When modifying a string, you can convert the string to [] byte for modification.
  • [] byte and string can be cast to each other.

5-String splicing

hammer := "kaka"
sickle := "molaiwei"

// Declare byte buffer
var stringBuilder bytes.Buffer

// Write string to buffer
stringBuilder.WriteString(hammer)
stringBuilder.WriteString(sickle)

// Output buffer as string
fmt.Println(stringBuilder.String())

6 - formatted output: fmt.Printf()

Table 1: common verbs and functions in string formatting
Move   WordsMerit   can
%vOutput by original value of value
%+vAt% v   On this basis, expand the structure field name and value
%#vOutput values in Go language syntax format
%TType and value of output Go language syntax format
%%Output%   noumenon
%bIntegers are displayed in binary
%oIntegers are displayed in octal
%dIntegers are displayed in decimal
%xIntegers are displayed in hexadecimal
%XIntegers are displayed in hexadecimal and capital letters
%UUnicode   character
%fFloating point number
%pPointer, hexadecimal display

 

Keywords: Go git github

Added by stormcloud on Tue, 30 Nov 2021 16:41:47 +0200