golang learning notes 003 -- variables and data types

1. Variables

1.1 three ways to use variables

● the first: specify the variable type. If no assignment is made after declaration, the default value is used
● the second: self judgment according to the value (type derivation)
● the third: omit var. Note: = the variables on the left should be undeclared, otherwise the compilation error will occur

	package main
	
	import "fmt"//ftm takes care to provide formatting, input and output functions
	
	func main(){
		//Variable declaration
		//First kind
		var i int
		i=10
		fmt.Println(i)
		//Second
		var m =10.1
		fmt.Println(m)
		//Third
		name := "wang"//Equivalent to var name string name = "Wang"
		fmt.Println(name)
	}

1.2 multivariable declaration

	//Declare multiple objects at once
	//First kind
	var n1,n2,n3 int
	fmt.Println(n1,n2,n3)
	//Second
	var m1,m2,m3 = 1,2,3
	fmt.Println(m1,m2,m3)
	//Third
	a1,a2,a3 :=100,"wang",10.2
	fmt.Println(a1,a2,a3)

1.3 global variables

	//Define global variables
	var aa1=100
	var aa2="ww"
	//Recommended use
	var (
		aa3=200
		aa4="ee"
	)

2. Data type

2.1 numerical type

2.1.1 integer type

● check the variable type and number of bytes
●fmt.Printf("type of a1 is% T, number of bytes occupied by A2 is% d", a1,unsafe.Sizeof(a2))
● output: the type of a1 is int, and the number of bytes occupied by A2 is 16

2.1.2 floating point


explain:
● floating point numbers are signed
● the mantissa may be lost, resulting in loss of accuracy
● floating point type has a fixed range and length and is not affected by OS
● floating point type is float64 by default
● floating point type has two representations: decimal and scientific counting

	//Decimal number form: e.g. 5.12.512 (must have decimal point)
	num6 := 5.12
	num7 := .123//=> 0.123
	fmt.Println("num6=",num6,"num7=",num7)//num6= 5.12 num7= 0.123
	//Scientific counting form
	num8 :=5.1234e2 //?  5.1234 * the second power of 10
	num9 := 5.1234E2//? 5.1234 * the second power of 10
	num10 := 5.1234E-2 //? 5.1234/10 to the power of 0.051234
	fmt.Println("num8=",num8,"num9=",num9,"num10=",num10)//num8= 512.34 num9= 512.34 num10= 0.051234

2.2 character type

● there is no special character type in go language. If you want to store a single character, you usually use byte
● go language characters are connected by a single byte

	var c1 byte='a'
	var c2 byte='0'
	//When we directly output the byte value, we output the code value of the corresponding character
	fmt.Println("c1=",c1,"c2=",c2)//c1= 97 c2= 48
	//If you want to output the corresponding characters, you need to format the output
	fmt.Printf("c1=%c c2=%c\n",c1,c2)//c1=a c2=0
	//var c3 byte = 'North' / / overflow overflow
	var c3 int='north'
	fmt.Printf("c3=%c c3 Corresponding code value=%d",c3,c3)//c3 = corresponding code value of North c3 = 21271

● go language uses utf-8 encoding, with one byte for English and three bytes for Chinese characters
● in go, the character is essentially an integer. When directly output, it is the code value of utf-8 encoding corresponding to the character
● you can directly assign a number to a variable, and then format the output%c, the Unicode code corresponding to the number will be output
● the character type can be calculated, which is equivalent to an integer, because it corresponds to Unicode code

2.3 boolean type

● only true and false values are allowed
● occupy one byte

2.4 String type

● a string is a character sequence connected by a string of fixed length characters. go language strings are connected by a single byte, using utf-8
● once the string is assigned, the string cannot be modified. In go, the string is immutable
● var str ="hello"
● str[0] = 'a' the contents of str cannot be modified here, and the string in go is immutable
● two representations of strings
○ double quotation marks, can recognize escape characters
○ backquotes are displayed in the native form of strings, including line breaks and special characters, which can prevent attacks, output source code and other effects

2.5 default value of basic data type

● integer: 0
● floating point type: 0
● string: "
● boolean type: false

2.6 conversion between basic data types

● go language is different from java. When assigning values between different variable types, go needs to display conversion. Cannot convert automatically
● T(V) converts the value V to type T

	var i int32=100
	var n1 float32=float32(i)
	var n2 int8=int8(i)
	var n3 int64=int64(i)
	fmt.Printf("i=%v n1=%v n2=%v n3=%v",i,n1,n2,n3)//i=100 n1=100 n2=100 n3=100

matters needing attention
● data type conversion can be from small range – > large range to large range – > small range
● what is converted is the data stored in the variable, that is, the value, and the data type of the variable itself has not changed
● during conversion, such as converting int64 to int8[-128 – 127], no error will be reported during compilation, but the conversion result is treated as overflow, which is different from the desired result. Therefore, the range needs to be considered during conversion

	var n1 int32=12
	var n2 int8
	var n3 int8
	n2=int8(n1)+127//If the compilation passes, but the result is not 127 + 12, it will be treated as overflow
	n3=int8(n1)+128//Compilation failed
	fmt.Println(n2,n3)
	var n1 int32=12
	var n2 int64
	var n3 int8

	n2=n1+20//Int32 -- > Int64 error
	n3=n1+20//Int32 -- > int8 error

2.7 conversion of basic data type and String

2.7.1 mode 1: FMT Sprintf ('% parameter', expression)

	var num1 int=90
	var num2 float64=23.345
	var b bool=true
	var c byte='j'
	var str string

	str=fmt.Sprintf("%d",num1)
	fmt.Printf("str type %T str =%q\n",str, str)//str type string str ="90"

	str=fmt.Sprintf("%f",num2)
	fmt.Printf("str type %T str =%q\n",str, str)//str type string str ="23.345000"

	str=fmt.Sprintf("%t",b)
	fmt.Printf("str type %T str =%q\n",str, str)//str type string str ="true"

	str=fmt.Sprintf("%c",c)
	fmt.Printf("str type %T str =%q\n",str, str)//str type string str ="j"

2.7.2 method 2: use strconv package function

	var num3 int=99
	var num4 float64=23.3453
	var num5=232
	var b2 bool
	var str1 string

	str1=strconv.FormatInt(int64(num3),10)
	fmt.Printf("str1 type %T str1 =%q\n",str1, str1)//str1 type string str1 ="99"	
	
	str1=strconv.FormatFloat(num4,'f',10,64)//'f' format 10: it means that 10 decimal places are reserved. 64: it means that the decimal place is float64
	fmt.Printf("str1 type %T str1 =%q\n",str1, str1)//str1 type string str1 ="23.3453000000"

	str1=strconv.FormatBool(b2)
	fmt.Printf("str1 type %T str1 =%q\n",str1, str1)//str1 type string str1 ="false"

	str1=strconv.Itoa(int(num5))
	fmt.Printf("str1 type %T str1 =%q\n",str1, str1)//str1 type string str1 ="232"

2.8 converting string type to basic data type

	var str string ="true"
	var b bool
	b,_=strconv.ParseBool(str)
	//trconv. The parsebool (STR) function will return two values (value bool, err, error). We just want to get the first value. Err can be used_ ignore
	fmt.Printf("b type %T b=%v\n",b,b)//b type bool b=true

	var str2 string="1234"
	var n1 int64
	var n2 int
	n1,_=strconv.ParseInt(str2,10,64)//b type bool b=true
	
	n2=int(n1)
	fmt.Printf("n1 type %T  n1=%v\n",n1,n1)//n1 type int64  n1=1234
	fmt.Printf("n2 type %T  n2=%v\n",n2,n2)//n2 type int  n2=1234

	var str3 string ="123.3"
	var f1 float64
	f1,_=strconv.ParseFloat(str3,64)//n2 type int  n2=1234
	fmt.Printf("f1 type %T  f1=%v\n",f1,f1)//f1 type float64  f1=123.3

matters needing attention:
● when converting string to basic data type, ensure that string type can be converted to valid data. If hello is converted to an integer, the result will be 0. When converting bool type, the result will be false when not bool

Keywords: Go Back-end

Added by CPInteract on Mon, 24 Jan 2022 08:53:38 +0200