#### Go variables and basic data types (two)
today to learn about the basic data types, including integer type, decimal type, character type
##### integer type
used to store integer values, such as 0,1, -10 , 2222 and the like;
integer types are:
Types of |
Whether the symbol |
Take up storage space |
Table number range |
int8 |
Have |
1 byte |
-128~127 |
int16 |
Have |
2 bytes |
-2^15~2^15-1 |
int32 |
Have |
4 bytes |
-2^31~2^31-1 |
int64 |
Have |
8 bytes |
-2^63~2^63-1 |
Case:
main Package Import ( "FMT" "the unsafe" ) FUNC main () { // signed integer type (int default platform system according to the relevant type, 32-bit system Int32 = int // 64-bit system Int64 = int var I int . 1 = var I1 = int8 127 fmt.Println (I, I1) fmt.Println (unsafe.Sizeof (I)). 8 // variable memory size in bytes fmt.Println (unsafe.Sizeof (i1)) // a variable memory size in bytes }
Unsigned integer type
Types of |
Whether the symbol | Take up storage space | Table number range |
uint8 |
no |
1 byte |
0~255 |
uint16 |
no |
2 bytes |
0~2^16-1 |
uint32 |
no |
4 bytes |
0~2^32-1 |
uint64 |
no |
8 bytes |
0~2^64-1 |
Case:
main Package Import ( "FMT" "the unsafe" ) FUNC main () { // No title integer type (type uint with the system default internet related case, 32-bit system UInt32 = uint // 64-bit system UInt64 = uint var I uint 266 = var UInt16 I1 = 100 fmt.Println (I, I1) fmt.Println (unsafe.Sizeof (I)) //. 8 fmt.Println (unsafe.Sizeof (I1)) // 2 }
Other types of integer type
Types of |
Whether the symbol |
Take up storage space |
Table number range |
int |
Have |
4-byte 32-bit system, 64-bit byte 8 |
-2^31~2^31-1,-2^63~-2^63-1 |
uint |
no |
4-byte 32-bit system, 64-bit byte 8 |
0~2^32-1,0~2^64-1 |
rune |
Have | 4 bytes |
-2 ^ 31 to 2 ^ 31-1, represents a code unicode |
byte |
no | 1 byte |
0 to 255 using the byte storing the character when |
main Package Import "FMT" FUNC main () { var I = 1000 // unsigned int, the size of the memory with the Platform For var i1 uint = 1 // signed with the memory size Platform For var i2 byte = 255 // Word section type, memory size is 1 byte fmt.Println (I, I1, I2) }
Details integer
1. int uint system memory size and relevant;
2. Go declare default integer type int
3. In the case of a program to ensure correct operation, try using a small footprint data types
main Package Import "FMT" FUNC main () { // the default type int var = n-100 // formatted output fmt.Printf ( "type n1 is% T \ n, n) // % T as a Type // small footprint data type var = Age byte 21 is fmt.Println (Age) }
Type ##### decimal
decimal decimal type is used to store, eg: 1.1,2.2,3.1414;
Types of |
Take up storage space |
Table number range |
Single-precision float32 |
4 bytes |
-3.403E38 ~ 3.403E38 |
Double float64 |
8 bytes |
-1.798E308 ~ 1.798E308 |
main Package Import "FMT" FUNC main () { var A = 12.3 float32 var A1 = -0.01 float32 var float64 A2 = -0.02 fmt.Println (A, A1, A2) var A3 = 1.00000088 float32 var A4 = 1231.1111111 float32 var A5 = 11231.1111111 float32 fmt.Println (A3) //1.0000008, the mantissa portion is lost, the loss of precision fmt.Println (a4) //1231.1111, the mantissa portion is lost, the loss of precision fmt.Println (a5) //11231.111, the mantissa portion is lost, precision loss }
Details decimal type
1. Go floating-point type and has a fixed field length range, the system is not particularly affect;
2. Go float64 default floating-point type;
3. the development recommended float64;
4. two floating-point type constant species representation;
4.1 decimal form: 0.1415926 can be written as: .1415926 (*** *** must decimal);
4.2 scientific notation: 0.14e2 represents a power of 2 = 0.14 * 10 14;
4.3 scientific notation : 0.14e-2 represents the second power of 0.14 = 0.0014 / 10;
##### character types
Go no special character type, to be stored as a single character (letter) is generally used to save byte;
string is a string character sequence of fixed-length character connecting, Go is connected by a single byte string with traditional string made up of characters, the strings of bytes Go;
main Package Import "FMT" FUNC main () { // byte single quotation marks var C = byte 'A' fmt.Println (C) direct printing // byte value, that the output of the byte code value corresponding unicode // output corresponding character requires formatting fmt.Printf ( "C% \ n-", C) // overflow overflow // var c1 byte = 'your' var int = C1 'your' fmt.Printf ( "% C1 = c c1 corresponding code value D% \ n-", C1, C1) }
Details character type
1. If the character is stored in the ASCII table [0-9, az, AZ], may be used directly byte;
2. if the stored value corresponding to the character code is larger than 255 (byte indicates the maximum number of 255 = int8 ), may be considered represent a large range of types;
3. If you want to output character, need to format the output fmt.Printf ( "% C", C);
4. character constant is represented by a single character enclosed in single quotes;
5. the Go in allowing escape character '\' characters into the subsequent special character types: such as: var c byte = '\ n ' represents a line break;
6. the use UTF-8 encoding Go: a letter of 1 byte , a Chinese character is 3 bytes;
7. type of character can participate in operation: the equivalent of an integer
package main import "fmt" func main(){ var a byte = 'a' fmt.Println(10+a) // 107 = 10 + 97 }
-------------- recently updated simultaneously micro-channel public number: "pickles appetizer", we can focus on exchange