当前位置:网站首页>Integers and operators in go data types (3)

Integers and operators in go data types (3)

2022-06-23 08:17:00 weixin_ fifty-nine million two hundred and eighty-four thousand

integer

Integer is the most basic data type in all programming languages ,Go The language supports the following integer types by default :

type length ( Company : byte ) explain Range of values The default value is
int81 Signed 8 An integer -128~1270
uint81 Unsigned 8 An integer , And  byte Type equivalence 0~2550
int162 Signed 16 An integer -32768~327670
uint162 Unsigned 16 An integer 0~655350
int324 Signed 32 An integer , And  rune Type equivalence -2147483648~21474836470
uint324 Unsigned 32 An integer 0~42949672950
int648 Signed 64 An integer -9223372036854775808~92233720368547758070
uint648 Unsigned 64 An integer 0~184467440737095516150
int32 Bit or 64 position Platform specific Platform specific 0
uint32 Bit or 64 position Platform specific Platform specific 0
uintptr Same as corresponding pointer Unsigned integer , Unresolved bits sufficient to store pointer values 32 Under the platform 4 byte ,64 Under the platform 8 byte 0

Go The supported integer types are very rich , You can set the appropriate integer type as needed , To save memory space , Besides  int  and  int32  stay Go Language is considered to be two different types ( Empathy ,int  and  int64  It's also a different type ), The compiler won't help you with automatic type conversion , For example, the following example will have compilation errors :

var intValue1 int8
intValue2 := 8   // intValue2  Will be automatically deduced as  int  type  
intValue1 = intValue2  //  Compile error 

Compilation errors are similar to :

 cannot use intValue2 (type int) as type int8 in assignment

This compilation error can be solved by using cast :

intValue1 = int8(intValue2)) //  Compile and pass 

notes : As for type conversion, we will introduce it separately after introducing all data types .

 

We can also go through  intValue := uint8(intValue2)  This method performs both type conversion and assignment operations .

Besides , Like any programming language , You can add a prefix  0  To represent octal numbers ( Such as :077), Add prefix  0x  To represent a hexadecimal number ( Such as :0xFF), And the use of  E  To express 10 The tandem of ( Such as :1E3 = 1000).

Operator

Arithmetic operator

Go The language supports all conventional integer four arithmetic operations :+-*/  and  %( The remainder operation can only be used for integers ), But because of the strong typing , stay Go In language , Different types of integer values cannot be directly arithmetically operated , For example, the following calculation will report compilation errors :

intValue3 := intValue1 + intValue2

The compilation error message is as follows :

invalid operation: intValue1 + intValue2 (mismatched types int8 and int)

After the type conversion :

intValue3 := intValue1 + int8(intValue2)

If you are learning from a dynamic language Go, When you start writing code, you should pay particular attention to these types of problems bug.

stay Go In language , Self increment is also supported / Self - subtracting operator , namely  ++/--, But only as a statement , Cannot be used as an expression , And can only be used as a suffix , Can't put in front of variables :

intValue1++  //  It works ,intValue1  The value of the into  9
intValue1 = intValue1++ //  Invalid , Compiler error 
--intValue1  //  Invalid , Compiler error 

And support  +=-=*=/=%=  This quick way of writing :

intValue1 += intValue1 // 18
intValue1 -= intValue1 // 0
intValue1 *= intValue1 // 81
intValue1 /= intValue1 // 1
intValue1 %= intValue1 // 0

Comparison operator

Go The language supports the following common comparison operators : ><==>=<=  and  !=, The result of the comparison operator is a Boolean value .

Such as Last tutorial said ,Go It's a strongly typed language , Values of different types cannot be compared together , Otherwise, a compilation error will be reported :

if intValue1 == intValue2 {
    fmt.Println("intValue1  and  intValue2  equal ")
}

Only values of the same type can :

if intValue1 < intValue3 {

fmt.Println("intValue1  Than  intValue3  Small ")

}

thus it can be seen , All comparison operators will consider the data type factor when comparing , So there is no need to introduce additional similar PHP And other dynamic languages  ===  and  !==  This strict comparison operator .

however , All types of integer variables can be directly compared with literal constants , such as :

if intValue1 == 8 {
    fmt.Println("intValue1 = 8")
}

An operator

Bitwise operators operate on numeric values in binary form , More efficient , Better performance ,Go The following bit operators are supported by the :

Operator meaning result
x & y Bitwise AND hold x and y All for 1 The bit of is set to 1
x | y Press bit or hold x or y by 1 The bit of is set to 1
x ^ y Bitwise XOR hold x and y One for 1 One for 0 The bit of is set to 1
^x According to the not hold x In Chinese, it means 0 The bit of is set to 1, by 1 The bit of is set to 0
x << y Move left hold x The position in is shifted to the left y Time , Each move is equivalent to multiplying by 2
x >> y Move right hold x The position in moves to the right y Time , Each move is equivalent to dividing by 2

We can do some simple tests :

var intValueBit uint8
intValueBit = 255
intValueBit = ^intValueBit //  According to the not 
fmt.Println(intValueBit)   // 0
intValueBit = 1
intValueBit = intValueBit << 3 //  Move left  3  position , Equivalent to times  2^3 = 8
fmt.Println(intValueBit)       // 8

Logical operators

Go The language supports the following logical operators :

Operator meaning result
x && y Logic and operators (AND) If x and y All are true, The result is true, Otherwise, the result is false
x || y Logical or operator (OR) If x or y yes true, The result is true, Otherwise, the result is false
!x Logical nonoperator (NOT) If x by true, The result is false, Otherwise, the result is true

The result of a logical operator is also a Boolean value , Usually we can combine logical operators and comparison operators :

if intValue1 < intValue3 && intValue1 == 8 {
    fmt.Println(" Condition is true ")
}

Operator priority

It's described above Go The precedence of language operators is as follows ( From top to bottom, the priority is from high to low , Or the larger the number , The higher the priority ):

6      ^( According to the not ) !
5      *  /  %  <<  >>  &  &^
4      +  -  |  ^( Bitwise XOR )
3      ==  !=  <  <=  >  >=
2      &&
1      ||

++  or  --  Can only appear in statements , Cannot be used in expressions , Therefore, it does not participate in priority judgment .

原网站

版权声明
本文为[weixin_ fifty-nine million two hundred and eighty-four thousand]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/174/202206230749172375.html