Variable & basic data type


  1. Specifies the variable type. If it is not assigned, the default value is used. string is empty by default, and int and floating point number are 0 by default

  2. Automatically judge the type according to the value

  3. Omit var, note: the variable on the left of = should be undeclared

    var i int //Type 1
    i = 10
    var num = 10.36  //Automatic judgment type
    name := "jack"   //Equivalent to var num = "Jack"
    var n1,n2,n3  int
    var n1,n2,n3 = 100, "tom", 3.2
    n1,n2,n3 := 100, "tom", 3.2
  4. Global variables are defined outside the function

    package main
    import "fmt"   
    //n1-4 are global variables
    var n1 = 1
    var n2 = 2
        n3 = 100
        n4 = 200
    	func main() {
  5. Global variables can be declared without using them. All other variables need to be used (note that they are used instead of assignment)

  6. Blank identifier_ It is also used to discard values, such as value 5 in:, B = abandoned in 5, 7.

    _ It's actually a write only variable. You can't get its value. This is because you have to use all declared variables in the Go language, but sometimes you don't need to use all the return values from a function.

data type

  1. Basic data type

    • Numeric:

      • Integer -- > int (depending on OS),int8,int16,int32,int64... The digits are distinguished by the following numbers. The integer type is declared as int by default

      • Floating point ----- > float32, float64... The default is 64 bits. The floating point type has a fixed range and field length, which is not affected by the specific OS. It is recommended to use float64, and the default value is 0

    • bool in Boolean -- > go only allows true or false, and default false is not allowed to be represented by 0-1

    • Complex to be completed

    • Character type -- > go has no special character type. It uses byte (equivalent to uint8) to save single alphabetic characters. Chinese characters cannot be stored in byte, because Chinese characters account for three bytes under UTF-8. Go coding is unified, and UTF-8 is used without garbled code

      Run: equivalent to int32, which represents a Unicode code point. It is often used to process Chinese characters. 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, the \ u prefix will be added; Prefix \ u is always followed by a hexadecimal number of length 4, and prefix \ u is followed by a hexadecimal number of length 8

      var ch int = '\u0041'
      var ch2 int = '\u03B2'
      var ch3 int = '\U00101234'
      fmt.Printf("%d - %d - %d\n", ch, ch2, ch3) // When used with a character,% v or% d outputs an integer representing the character
      fmt.Printf("%c - %c - %c\n", ch, ch2, ch3) // The format specifier% c is used to represent characters
      fmt.Printf("%X - %X - %X\n", ch, ch2, ch3) // UTF-8 bytes
      fmt.Printf("%U - %U - %U", ch, ch2, ch3) //%U output string in U+hhhh format
      65 - 946 - 1053236
      A - β - r
      41 - 3B2 - 101234
      U+0041 - U+03B2 - U+101234
    • String (string) -- > officially, it is attributed to the basic data type. In go, the string is immutable and empty by default. A string is a fixed length array of bytes

      Using backquotes ` ` to output characters in the native form does not recognize escape characters, which can prevent attacks on the output source code. Using double quotes "" will recognize escape characters
      str1 = "hello\n"		//Will wrap
      //The following will be output as is, and the tab will also be output
      str2 = `                

      String splicing

      str1 = "hello" + "world"
      str2 = "hello" +			//Multi line splicing + sign left on the previous line
    • Basic data type conversion ----- > go cannot be implicitly converted. Conversion must be displayed

      var i int32 = 100
      var n float32 = float32(i)
      var n1 int32 = 12
      var n2 int64
      n2 = n1 + 20    //n1 is int32, and n1+20 is int32
      var n1 int32 = 12
      var n2,n3 int8
      n2 = int8(n1) + 127  //Can compile through
      n3 = int8(n1) + 128   //Compilation failed
    • Basic data type and string class conversion (pay attention to ensure that string type can be converted to valid data. If it cannot be converted, the compiler will convert it to default value)

      /*#Basic data type to string*/
      var num1 int = 99
      var num2 float32 = 23.3
      var b bool = true
      var myChar byte = 'h'
      var str string
      //Method 1: fmt.Sprintf function 
      str = fmt.Sprintf("%d",num1)
      str = fmt.Sprintf("%f",num2)
      str = fmt.Sprintf("%c",myChar)
      //Method 2: corresponding functions in strconv package
      str = strconv.FormatInt(int64(num1),10)          //10 indicates decimal output
      str = strconv.FormatFloat(num2,'f',10,64)       //f represents the format ddd.dddd,10 represents the precision, - 1 represents the least precision, and 64 represents float64
      str = strconv.Itoa(num1)                        //Boundary method of int transformation
      /*#string Transfer to basic data type*/
      var str string = "true"
      var b bool
      b, _ =strconv.ParseBool(str)				//This function has two return values. We only need bool, so we ignore the other return value
  2. Derived / complex data types

    • Pointer
    • array
    • Structure (struct)
    • Channel
    • Function (also a type)
    • Slice
    • Interface
    • map

Posted on Mon, 06 Dec 2021 21:19:05 -0500 by gammaster