4.5.2.5.md

This commit is contained in:
Unknown
2013-06-09 20:18:11 +08:00
parent 146c81bd01
commit 4de688c86a
7 changed files with 114 additions and 1 deletions

View File

@@ -34,6 +34,7 @@ Golang中国[golang.tc](http://www.golang.tc/)
- 协助翻译: - 协助翻译:
- [zhanming](https://github.com/zhanming) - [zhanming](https://github.com/zhanming)
- [themorecolor](https://github.com/themorecolor) - [themorecolor](https://github.com/themorecolor)
- [everyx](https://github.com/everyx)
##授权许可 ##授权许可
除特别声明外,本书中的内容使用 [CC BY-SA 3.0 License](http://creativecommons.org/licenses/by-sa/3.0/)(创作共用 署名-相同方式共享3.0 许可协议)授权,代码遵循 [BSD 3-Clause License](https://github.com/astaxie/build-web-application-with-golang/blob/master/LICENSE.md)3 项条款的 BSD 许可协议)。 除特别声明外,本书中的内容使用 [CC BY-SA 3.0 License](http://creativecommons.org/licenses/by-sa/3.0/)(创作共用 署名-相同方式共享3.0 许可协议)授权,代码遵循 [BSD 3-Clause License](https://github.com/astaxie/build-web-application-with-golang/blob/master/LICENSE.md)3 项条款的 BSD 许可协议)。

View File

@@ -191,7 +191,7 @@ Example 4.9 [casting.go](examples/chapter_4/casting.go)
不过如果你实际存的数字超出你要转换到的类型的取值范围的话,则会引发 panic第 13.2 节)。 不过如果你实际存的数字超出你要转换到的类型的取值范围的话,则会引发 panic第 13.2 节)。
**问题 4.1 int 和 int64 是相同的类型吗?** **问题 4.1** int 和 int64 是相同的类型吗?
###4.5.2.2 复数 ###4.5.2.2 复数
Go 拥有以下复数类型: Go 拥有以下复数类型:
@@ -313,6 +313,34 @@ Go 中拥有以下逻辑运算符:`==`、`!=`(第 4.5.1 节)、`<`、`<=`
b3:= 10 > 5 // b3 is true b3:= 10 > 5 // b3 is true
###4.5.2.5 算术运算符 ###4.5.2.5 算术运算符
常见可用于整数和浮点数的二元运算符有 `+``-``*``/`
相对于一般规则而言Go 在进行字符串拼接时允许使用对运算符 `+` 的重载,但 Go 本身不允许开发者进行自定义的运算符重载)
`/` 对与整数运算而言,结果依旧为整数,例如:`9 / 4 -> 2`
取余运算符只能作用于整数:`9 % 4 -> 1`
整数除以 0 可能导致程序崩溃,将会导致运行时的恐慌状态(如果除以 0 的行为在编译时就能被捕捉到,则会引发编译错误);第 13 章将会详细讲解如何正确地处理此类情况。
浮点数除以 0.0 会返回一个无穷尽的结果,使用 `+Inf` 表示。
**练习 4.4** 尝试编译 [divby0.go](exercises/chapter_4/divby0.go)。
你可以将语句 `b = b + a` 简写为 `b+=a`,同样的写法也可用于 `-=``*=``/=``%=`
对于整数和浮点数,你可以使用一元运算符 `++`(递增)和 `--`(递减),但只能用于后缀:
i++ -> i += 1 -> i = i + 1
i-- -> i -= 1 -> i = i - 1
同时,带有 `++``--` 的只能作为语句,而非表达式,因此 `n = i++` 这种写法是无效的,其它像 `f(i++)` 或者 `a[i]=b[i++]` 这些可以用于 C、C++ 和 Java 中的写法在 Go 中也是不允许的。
在运算时 **溢出** 不会产生错误Go 会简单地将超出位数抛弃。如果你需要范围无限大的整数或者有理数(意味着只被限制于计算机内存),你可以使用标准库中的 `big` 包,该包提供了类似 `big.Int``big.Rat` 这样的类型(第 9.4 节)。
###4.5.2.6 随机数
一些像游戏或者统计学类的应用需要用到随机数。`rand` 包实现了伪随机数的生成。
##啊哦,亲,你看得也太快了。。。还没翻译完呢 0 0 ##啊哦,亲,你看得也太快了。。。还没翻译完呢 0 0
要不等到 ***2013 年 6 月 11 日*** 再来看看吧~~ 要不等到 ***2013 年 6 月 11 日*** 再来看看吧~~

View File

@@ -0,0 +1,22 @@
package main
import (
"fmt"
"unicode/utf8"
)
func main() {
// count number of characters:
str1 := "asSASA ddd dsjkdsjs dk"
fmt.Printf("The number of bytes in string str1 is %d\n",len(str1))
fmt.Printf("The number of characters in string str1 is %d\n",utf8.RuneCountInString(str1))
str2 := "asSASA ddd dsjkdsjsこん dk"
fmt.Printf("The number of bytes in string str2 is %d\n",len(str2))
fmt.Printf("The number of characters in string str2 is %d",utf8.RuneCountInString(str2))
}
/* Output:
The number of bytes in string str1 is 22
The number of characters in string str1 is 22
The number of bytes in string str2 is 28
The number of characters in string str2 is 24
*/

View File

@@ -0,0 +1,8 @@
package main
func main() {
a, b := 10, 0
c := a / b // panic: runtime error: integer divide by zero
print(c)
}

View File

@@ -0,0 +1,18 @@
package main
var a string // global scope
func main() {
a = "G"
print(a)
f1()
}
func f1() {
a := "O" // new local variable a, only scoped within f1() !
print(a)
f2()
}
func f2() {
print(a) // global variable is taken
}
// GOG

View File

@@ -0,0 +1,19 @@
package main
var a = "G" // global scope
func main() {
n()
m()
n()
}
func n() {
print(a)
}
func m() {
a = "O" // simple assignment: global a gets a new value
print(a)
}
// GOO

View File

@@ -0,0 +1,17 @@
package main
var a = "G" // global (package) scope
func main() {
n()
m()
n()
}
func n() {
print(a)
}
func m() {
a := "O" // new local variable a is declared
print(a)
}
// GOG