variable
How to use variables
- Specify the variable type, if the declaration does not assign a value, use the default value
var i int
- Determine the variable type by itself according to the value
var num = 10.11
- omission var
name := "tom"
//等价于
var name string
name = "tom"
Note: The variable on the left side of **:=** should not have been declared, otherwise there will be a compilation error
multivariate declaration
- Method 1: same type
var n1,n2,n3 int
//等价于
var n1 int
var n2 int
var n3 int
- Method 2: different types
var n1,name,n3 = 100,"tom",888
- Method 3: Type derivation
n1,name,n3 := 100,"tom",888
global variable declaration
var n1 = 100
var n2 = 200
var name = "jack"
//等价于
var(
n1 = 100
n2 = 200
n3 = "jack"
)
Shaping usage details
- The types of integers in Go are divided into: signed and unsigned, the size of int and uint is related to the system
- Go's integer type is declared as int by default
- View the data type and byte size of a variable in the program
var n1 = 100
fmt.Printf("n1 的类型 %T,n1占用的字节数是 %d",n1,unsafe.Sizeof(n1))
- Go programs try to use the data types that occupy less space while ensuring the normal operation of the program
- bit: The smallest storage unit in a computer. byte: the basic storage unit in a computer
Floating-point usage details
- Floating-point numbers are stored in the machine in the form: floating-point number = sign number + exponent bit + mantissa bit
- The mantissa part may be lost, resulting in a loss of precision
- Go floating point types have fixed range and field length, independent of OS
- Go floating point type is declared as float64 by default
- Go float types support scientific notation
num :=2.1234e2// 5.1234 * 10的2次方
num :=2.1234E2// 5.1234 * 10的2次方
num :=2.1234E-2// 5.1234 / 10的2次方
- Usually use float64 type
Character Type Usage Details
There is no specific character type in Go, generally use byte type
When the byte value is directly output, it is the code value of the corresponding character that is output
If you need to output the corresponding characters, you need to format the output
var c1 byte = 'a'
var c2 byte = '0'
fmt.Printf("c1=%c,c2=%c\n",c1,c2)
- A character constant is a single character enclosed in single quotes ('')
var c1 byte = 'a'
var c2 int = '中'
var c3 byte = '9'
- Go allows the use of the escape character "\" to convert the following characters into special character constants
- Characters in Go language use UTF-8 to encode English letters----1 byte Chinese characters----3 bytes
- In Go, the essence of a character is an integer, and when output directly, it is the code value of UTD-8 corresponding to the character
- You can directly assign a number to a variable, and then press %c to format the output, and the unicode character corresponding to the number will be output
- The character type can be operated on, which is equivalent to an integer, because it corresponds to a Unicode code
Boolean usage details
-
The storage space occupied by bool type is one byte
-
The bool type can only take true or false
-
Cannot replace bool type with 0 or non-zero
String Type Usage Details
-
The bytes of strings in Go language use UTF-8 encoding to represent Unicode
-
Once a string is assigned, the string cannot be modified: strings are immutable in Go
-
Two representations of strings
1. Double quotes will recognize escape characters
2. Backticks ( `` ), output in the native form of strings, including newlines and special characters, can prevent attacks, output source code, etc.
-
String concatenation
var str = "hello " + "world"
When concatenating very long strings, you can write in separate lines (write the plus sign on the previous line)
var str = "hello" + "world" + "hello" + "world"
Data Conversion Usage Details
-
Data types in Go must be converted, not automatically converted
-
The conversion of data types in Go can be expressed from a small range ----> a large range, or a large range ----> a small range
-
The data stored in the variable (that is, the value) is converted, and the data type of the variable itself does not change
-
In the conversion, such as converting int64 to int8, the compilation will not report an error, but the conversion result is treated as overflow, which is different from the result we expected
Basic data type to String
Method 1: fmt.Sprintf
fmt.Sprintf("%参数",数据)
//例子
var num1 int = 99
var num2 float64 = 23.456
var myChar byte = 'h'
var str string
str = fmt.Sprintf("%d",num1)
str = fmt.Sprintf("%f",num2)
str = fmt.Sprintf("%c",myChar)
Method 2: strconv
//例子
var num1 int = 99
var num2 float64 = 23.456
var b bool = true
var str string
str = strconv.FormatInt(int64(num1),10)
str = strconv.FormatFloat(num2,'f',20,64)
str = strconv.FormatBool(b)
String to basic type
Method 1: strconv
var str string = "true"
var b bool
b,_ = strconv.ParseBool(str)
var str string = "1234590"
var n1 int
n1,_ = strconv.ParseInt(str,10,0)
Precautions:
- When converting the String type to a basic data type, ensure that the String type can be converted into valid data. If "hello" is converted into an integer, then Golang directly converts it to 0
constant
introduce
- Constants are modified with const
- When a constant is defined, it must be initialized
- Constants cannot be modified
- Constant smart modification bool, numeric type, string type
Notice
-
more concise writing
const ( a=1 b=2 )
-
There is also a professional way of writing
const( a=itoa b c ) // a=0,b=1,c=2
-
There is no specification in Go that constants must be capitalized
-
Still control the scope of variable access by the case of the first letter