The first article from the public number: Go programming time
“Go programming time”, a can take you to learn the Go language column, at the same time welcome to search my namesake public account [Go programming time] (beautiful layout is more suitable for reading), the first time to get Go language dry materials.
A series of reading
1. Set up the Go language development environment
2. Five ways to create variables in Go language
1. The integer
In Go, integer types can be further subdivided into 10 types, which I have compiled into a table for your convenience.
The difference between an int and a uint is that a u, with a U, means unsigned, and no u means signed.
Explain the difference between this symbol
Take Int8 and Uint8 as examples. 8 represents 8 bits and can represent 2^8 = 256.
Uint8 is unsigned and can represent all positive numbers, 0-255, just 256 numbers.
Int8 is signed, it can be positive or negative, so what do I do? Split it in half. -128-127, which is exactly 256 numbers.
Int8 int16 int32 int64 Each of these types has a number at the end, indicating that they can represent a fixed number of values.
Int doesn’t specify its number of bits, which means that its size can change. By what?
- When you’re on a 32-bit system, both ints and uints take up 4 bytes, or 32 bits.
- If you’re on a 64-bit system, both ints and uints take up 8 bytes, or 64 bits.
For this reason, in some cases you should avoid using int and uint in favor of the more precise int32 and int64, such as in binary transfers, reads and writes (to keep the structure of the file unaffected by the length of bytes on different compilation platforms).
Representation of different bases
As a matter of habit, when initializing variables of integer type, we use base 10 notation because it is the most intuitive, such as this, to represent the integer 10.
var num int = 10
Copy the code
Be aware, however, that you can use other bases to represent an integer, using the more common bases of 2, 8, and 16 as examples.
Base 2: prefix with 0B or 0b
var num01 int = 0b1100
Copy the code
Base 8: Prefix with 0O or 0O
var num02 int = 0o14
Copy the code
Hexadecimal: prefix with 0x
var num03 int = 0xC
Copy the code
The following code represents the value of base 10 in binary, base 8, and base 16:12
package main
import (
"fmt"
)
func main(a) {
var num01 int = 0b1100
var num02 int = 0o14
var num03 int = 0xC
fmt.Printf("The base 2 number %b represents: %d \n", num01, num01)
fmt.Printf("The base 8 number %o stands for: %d \n", num02, num02)
fmt.Printf("The hexadecimal number %X represents: %d \n", num03, num03)
}
Copy the code
The output is as follows
Base 2:1100:12 base 8:14:12 base 16: C: 12Copy the code
The above code uses the FMT package formatting function, you can refer to the above code
%b is represented as binary %c the corresponding Unicode code value %d is represented as decimal %o is represented as octal %q The value corresponds to a single quoted GO syntax character literal, with safe escape when necessary %x is represented as hexadecimal, using a-f%X is expressed in hexadecimal format using A -f %U in Unicode format: U+1234, equivalent to"U+%04X"%E is represented in scientific notation %f is represented in floating point notationCopy the code
2. Floating point
Floating-point values generally consist of integer parts and decimal points. And the decimal part.
The whole and fractional parts are expressed in base 10 notation. But there’s another way to write it. That is to add an exponent to it. The exponent part consists of “E” or “E” and a decimal number with a plus or minus sign. For example, 3.7E-2 represents the floating point number 0.037. For example, 3.7E+1 represents the floating point number 37.
Sometimes, the representation of floating-point type values can also be simplified. For example, 37.0 can be reduced to 37. For example, 0.037 can be simplified to.037.
It is important to note that in Go, the relevant parts of floating-point numbers can only be represented in base 10 notation, not in base 8 or base 16 notation. For example, 03.7 must represent the floating point number 3.7.
Float32 and float64
The Go language provides floating-point numbers float32 and float64 with two precision.
Float32, also known as single-precision, is stored in 4 bytes, i.e. 4*8=32 bits, of which 1 bit is used for symbol, 8 bits are used for exponent and the remaining 23 bits represent mantissa
Float64, also known as double precision, stores eight bytes, or 8*8=64 bits, of which one is used for symbols, 11 for exponents, and the remaining 52 bits for mantissa
So what does precision mean? How many significant bits are there?
The accuracy depends mainly on the number of digits in the mantissa.
In float32, the number of mantissa is 23 bits, except for all zeros, and is at least 2^-23 (1.19*10^-7). Float is accurate to the last six digits plus the significant digit before the decimal point.
Similarly, float64 (single precision) has a 52-digit mantissa with a minimum of 2^-52, which is about 2.22*10^-16, so it is accurate to 15 decimal places plus one place before the decimal point, giving 16 significant digits.
Through the above, the following points can be summarized:
Float32 and float64 can represent many values
Floating-point types can range from very small to very large. The limit of the range of floating-point values can be found in the Math package:
- Constant math.MaxFloat32 indicates the largest value that float32 can take, which is about 3.4e38;
- Constant math.MaxFloat64 Indicates the maximum value that float64 can take, which is about 1.8E308;
- The minimum values of FLOAT32 and float64 are 1.4E-45 and 4.9E-324 respectively.
Second, the value is large but the accuracy is limited
Although other people can represent the value is very large, but the precision bit is not so large.
- Float32 has an accuracy of only about six decimal numbers (representing six decimal places after scientific notation)
- Float64 has an accuracy of about 15 decimal numbers (representing 15 decimal places after scientific notation)
What do I mean by precision here?
For example, the number 10000018, represented in float32, is 1.0000018 * 10^7, which is accurate to 6 decimal places since it has 7 significant bits.
Now, in scientific notation, we have seven decimal places behind the decimal point, which just meets our accuracy requirements. What does that mean? If you do something like plus 1 or minus 1, you’re guaranteed to get the exact answer
import "fmt"
var myfloat float32 = 10000018
func main(a) {
fmt.Println("myfloat: ", myfloat)
fmt.Println("myfloat: ", myfloat+1)}Copy the code
The output is as follows
myfloat: 1.0000018 e+07
myfloat: 1.0000019 e+07
Copy the code
The above example shows a critical case of data that just meets the accuracy requirement. For comparison, the following example shows an example of data that just does not meet the accuracy requirement. We just have to add one more digit to this number.
If the value is changed to 100000187, the float32 float32 is also used to represent the scientific notation. Due to its limited accuracy, the 7 digits after the decimal point are accurate. However, if the value is mathematically calculated, the value in the seventh digit becomes inaccurate because the eighth digit cannot be represented.
Myfloat01 = 100000182 +5 myFloat02 = 100000187
import "fmt"
var myfloat01 float32 = 100000182
var myfloat02 float32 = 100000187
func main(a) {
fmt.Println("myfloat: ", myfloat01)
fmt.Println("myfloat: ", myfloat01+5)
fmt.Println(myfloat02 == myfloat01+5)}Copy the code
However, since its type is float32, the result of the final comparison is not equal (inaccurate from the seventh decimal place).
myfloat: 1.00000184 e+08
myfloat: 1.0000019 e+08
false
Copy the code
Because of the accuracy, myfloat == myfloat +1 will return true.
Reference article:
www.zhihu.com/question/26…