当前位置:网站首页>Go language slow start -- go operator
Go language slow start -- go operator
2022-07-27 15:39:00 【zy010101】
go Operator
go Most operators of are found in most other programming languages . The two operands involved in binary operators that need attention must be the same .
- If these two operands are type specific values , Then their types must be the same , Or one of the operands can be implicitly converted to the type of the other operand .
- If only one of the operands is typed , Then either another type indefinite operand can represent the value of the type that determines the operand for this type , Either any value of the default type of this type indefinite operand can be implicitly converted to the type of this type definite operand .
- If both operands are of uncertain type , Then they must both be Boolean values , Both are string values , Or both are basic numeric values . go The support for bit operation is relatively strong , It supports the clearing operation (&^), however go The current ecosystem has not developed embedded ,IOT Direction , Therefore, bit operations are not often used .
overflow
- A type determines that the value represented by a numeric constant cannot overflow the representation range of its type .
- The value represented by an indefinite numeric constant can overflow the representation range of its default type . When a type uncertain numeric constant value overflows the representation range of its default type , This value will not be truncated ( That is, rewind ).
- When converting a non constant numeric value to another numeric type , This non constant numeric value can overflow the type of conversion result . In this transformation , When overflow occurs , The conversion result is the truncation of the non constant numerical value ( That is, rewind ) Express .
Here are some examples :
package main
import "fmt"
func main() {
var a, b uint8 = 255, 1
var c = a + b // a and b All are uint8,go The compiler infers c It's also uint8 type
fmt.Println(c)
var d = a << b // a and b All are uint8,go The compiler infers d It's also uint8 type
fmt.Println(d, uint16(a)<<b) // Coercive transformation a by uint15, Then the data type of the overall operation result is uint16
var x = 1.2 + 3/2
}++ and – expression
stay python in , Support removes uncomfortable C/C++ Medium ++ and – Operator ; And in the go In language , They are no longer operators , It's an expression , And only the postpositive form . So the following code is wrong .
var a = 0
var b = a++ // error ,++ Is an expression , It's not worth it . We can only put it on a separate line .
a++ // correct ,++ Is an expression .remember ,– and ++ Is an expression , It's not an operator , And only post .++ It's self increasing ,– It's self reducing
About the result of arithmetic operation
Except shift operation , For a binary arithmetic operation ,
- If both of its operands determine the value for the type , Then the result of this operation is also a type determination value with the same type as the two operands .
- If only one operand is typed , Then the result of this operation is also a type determination value that is the same as the type of this type determination operand . Another type of uncertain operand will be inferred as ( Or implicitly convert to ) This type determines the type of operand .
- If both of its operands are type indeterminate , Then the result of this operation is also a type uncertain value . In operation , The types of the two operands will be assumed to be one of their default types ( Choose according to this priority :complex128 higher than float64 higher than rune higher than int). The default type of the result is also assumed for this type . such as , If a type is uncertain, the default type of operand is int, The default type of another type of uncertain operand is rune, Then the type of the former is also regarded as rune, The operation result is a default type rune The type uncertainty of . For shift operations , The result rule is a little complicated . First of all, the results of the shift operation must be integers .
- If the left operand is a typed value ( Then its type must be an integer ), Then the result of this shift operation is also a type determination value with the same type as the left operand .
- If the left operand is a type uncertain value and the right operand is a constant , Then the left operand will always be treated as an integer . If its default type is not an integer (rune or int), Then its default type will be treated as int. The result of this shift operation is also a type uncertain value, and its default type is consistent with that of the left operand .
- If the left operand is a type uncertain value and the right operand is a non constant , Then the left operand will first be converted to the expected assumption type of the operation result . If the expected assumption type is not specified , Then the default type of the left operand will be regarded as its expected assumption type . If this expected assumption type is not a basic integer type , Compile and report errors . Of course, the final result of the operation is a type. Therefore, it is expected to assume the type determination value of the type .
Reference material
https://gfw.go101.org/article/operators.html
边栏推荐
- The shell script reads the redis command in the text and inserts redis in batches
- Push down of spark filter operator on parquet file
- Leetcode 456.132 mode monotone stack /medium
- 【剑指offer】面试题45:把数组排成最小的数
- 【剑指offer】面试题56-Ⅰ:数组中数字出现的次数Ⅰ
- 直接插入排序
- Troubleshooting the slow startup of spark local programs
- 《吐血整理》C#一些常用的帮助类
- Network device hard core technology insider router Chapter 15 from deer by device to router (Part 2)
- [正则表达式] 匹配多个字符
猜你喜欢
随机推荐
Spark Bucket Table Join
扩展Log4j支持日志文件根据时间分割文件和过期文件自动删除功能
C语言:字符串函数与内存函数
Network equipment hard core technology insider router Chapter 10 Cisco asr9900 disassembly (III)
Spark RPC
C:浅谈函数
Network device hard core technology insider router Chapter 15 from deer by device to router (Part 2)
实现自定义Spark优化规则
Complexity analysis
Leetcode 781. rabbit hash table in forest / mathematical problem medium
js运用扩展操作符(…)简化代码,简化数组合并
Singles cup, web:web check in
直接插入排序
go语言慢速入门——基本内置类型
$router.back(-1)
Spark动态资源分配的资源释放过程及BlockManager清理过程
Multi table query_ Sub query overview and multi table query_ Sub query situation 1 & situation 2 & situation 3
股票开户佣金优惠,炒股开户哪家证券公司好网上开户安全吗
Leetcode 191. number of 1 bits bit operation /easy
Dan bin Investment Summit: on the importance of asset management!









