1.int类型和float类型不能匹配
go语言规定不允许在整数型变量和浮点型变量之间进行任何数学运算。错误例子如下:
参考:go语言中文文档:www.topgoer.com
转自:https://segmentfault.com/a/1190000023825004
var n, m = 21, 2.1 // 下面一行会报错:invalid operation: n / m (mismatched types int and float64) fmt.Println(n / m)
改正结果如下:
var n = 21 / 2.1 fmt.Println(n)
2.int类型和time.Duration类型不能匹配
go语言规定数值运算的操作数必须具有相同的类型,除非该操作包含类型转换或非类型化常量。错误例子如下:
var n = 3 // 下面一行会报错:invalid operation: timeout * time.Millisecond (mismatched types int and time.Duration) fmt.Println(n * time.Millisecond)
改正结果如下:
// 1. 使用常量 const n = 3 fmt.Println(n * time.Millisecond) /