Go core development study notes (c) - the basic data types

Variable data types:

  • Basic data types:

    • Integer {

      • int: plastic, 32-bit operating system occupies 4 bytes, 64-bit operating system occupies 8 bytes,
      • byte: equal unsigned int8,
      • int8, int16, int32, int64: 8bit integer, 16bit integer ... (01 represents a first positive and negative, so the table values int8 only seven, so the value range of -2 . 7 1-2 7-1),
      • uint8, uint16, uint32, uint64: unsigned integer 8bit, 16bit unsigned integer (unsigned it does not involve negative, so uint8 0 to 2 ^ 8-1),
      • rune: Int32 equal
        }
    • Float {

      • float32: single precision,
      • float64: double,
        }
    • Character: no special character, such as char, varchar, no! ! Using a single byte to store alphabetic characters, characters can not use the byte type, because the Chinese characters are 3 + bytes

    • Boolean BOOL: TRUE or FALSE

    • String string: Go Language attribution string as the basic data types

    • Plural: complex64 complex128 (currently not used)

  • Complex data types:

  1. Pointer pointer
  2. Array array
  3. Structure struct, class alternative
  4. Pipeline channel
  5. Function (also a function type)
  6. Slice slice
  7. Interface interface
  8. map (similar hashmap, set), relatively simple and not complicated, the equivalent of other language map, only this.
  • Details of integers:
  1. golang types into an integer int and uint, the range of values ​​is not the same.
  2. View a variable data type and size:
    package main
    
    import (
    	"fmt"
    	"unsafe"
    )
    
    func main() {
    	/*
    	printf用于做格式化输出,
    	unsafe.Sizeof()函数:用于输出一个变量所占字节大小
    	*/
    
    	var i = 100
    	fmt.Printf("i的数据类型为: %T\n",i)
    	fmt.Printf("i所占用的字节数为:%d",unsafe.Sizeof(i))
    }
    
  3. Given local conditions to verify the data type of a variable, such as age, most people will not be more than 255 years old, so var age byte on it, there is no need to assign an age int64, wasted a lot of byte variable.
    Also known as Little surety insurance principle, not sure if the number you use a little bigger, because now is not the current computer hardware capacity bottlenecks.
  • Floating-point tips:
  1. Floating-point data stored in the computer in three parts: a sign bit, the exponent bits mantissa bits.
  2. Stored procedures, mantissa part may be lost, resulting in loss of precision.
  3. More accurate than a single double precision floating point numbers typically used convention float64.
  4. Golang floating-point type and has a fixed field length range, influence from OS, and int is not the same, there is no default, and only float32 float64.
  5. Golang float default declared float64.
  6. a float must have a decimal point, for example, 0.123 can be written as num = .123.
  7. Support scientific notation: NUM: 5.1234e = (E) 2 // actually 512.34 , e (E) of 10 ^ X X-th power, the case does not affect the e scientific notation, X may be a negative number, x ^ - i.e., a 1 / x.
  • Character of tips:
  1. No char and varchar concept, if a single character, then the ASCII, byte type used to represent all individual characters, the character if the number is linked string: var c1 byte = 'a'.

  2. Golang string by bytes.

  3. If the stored character in the ASCII table, can be used directly byte output directly to the output code value; printf% c output using accurate ascii characters.

  4. If the stored character is greater than 255, the use of type int save, see e.g. characters code value, the code value may be used printf% d characters output.

  5. Character constant need to use 'enclosed.

  6. Golang, the nature of the character is an integer, when the direct output, a code value corresponding to the character utf-8 encoded.

  7. Similarly under section 6, the above can be directly assigned to a variable integer, formatted output% c, corresponding to the digital output of the uni-code characters.

  8. The strips 6, 7, it can be determined, calculation can be characters, character code value is added by this embodiment, the output format and% c, uni-code of the new character appears.
    For example:

    package main
    
    import "fmt"
    
    func main() {
    //Golang里面没有字符类型数据,只能用byte表示,即ASCII代码.
    //如果单纯输出,则输出为码值,'a'=97,不会输出字符a,而'9'=57
    var c1 byte = 'a'
    var c2 byte = '9'
    fmt.Println(c1,c2)
    //如果我们希望正确输出字符,则需要格式化输出
    fmt.Printf("c1=%c\tc2=%c\n",c1,c2)
    
    //如果输出汉字,那么byte内的汉字可以输出,而超过255则不可以再使用byte作为数据类型
    //var c3 byte = '赵'
    //fmt.Printf("c3=%c",c3)
    //上述是行不通的,赵已经超越了byte的最大值溢出了,所以需要int类型
    var c3 int = '赵'
    fmt.Printf("c3=%d\n",c3)
    var c4 int = 10 + 'b'
    fmt.Printf("c4=%c",c4)
    //c4的结果为1
    }
    

9. The character stored in the computer, the character code corresponding to a value (integer) required to find out.
Storage: Char -> finds the corresponding code value -> into binary -> memory
read: reading binary -> corresponding to the code value -> converted to a character -> read

character code values are specified in advance and good, not modify.
10. Go language coding are unified into utf-8, is no longer involved gbk, gb2312, garbage problem does not occur again.

Boolean bool of tips:

  1. Only two values: True and False, null values ​​can not take anything.
  2. Boolean one byte, use unsafe.Sizeof () Check variable size.
  3. Mainly for a logic operation, generally used for process control, if, for the like.

String string of tips:

  1. Many of the characters spliced ​​together as a string, Golang language string is a sequence of characters many characters connected.
  2. Using UTF-8 text encoding represents unicode, distortion is not a problem.
  3. Golang strings are immutable.
  4. String two forms:
    double quotes: "" identifies escape character, such as \ n will wrap is not output \ n this string effect
    backquote: `` primary output all the characters, the escape character fails, Similarly in python var1 = r ''
    package main
    
    import "fmt"
    
    func main() {
    //创建字符串,打印字符串
    var add string = "shit aaa!"
    fmt.Println(add)
    
    //Go中字符串不可变
    //var add1 string = "damn!"
    //add1[0] = 'a'
    //fmt.Println(add1)
    //这种切片改变字符串的方式不可以
    
    //字符串两种表现形式
    //双引号:"" 会识别转义字符,例如\n就是会换行,不会输出\n这种字符串效果
    //反引号:`` 原生输出所有字符,转义字符失效,类似python中 var1=r''
    s1,s2 := "abc\nabc\n",`abc\tabc`
    fmt.Println(s1,s2)
    //s1是abc换行abc, s2就是abc\tabc
    }
    
  5. String splicing:
    typically used to splice +.
    Multi-line stitching + must remain end of the line, and then turn the next line, or will be error
    `
    basic data type Default List:
    Integer 0
    Floating Point 0
    string" "
    boolean false
    package main
    
    import "fmt"
    
    func main() {
    var a int           //0
    var b int32         //0
    var c int64         //0
    var d bool          //false
    var e string        //""
    var f float32       //0
    var g float64       //0
    fmt.Println(a,b,c,d,e,f,g)
    //printf中%v表示按照变量值输出
    }
    
Published 50 original articles · won praise 18 · views 4027

Guess you like

Origin blog.csdn.net/weixin_41047549/article/details/89546000