From 4de688c86a3f457b80430ca21ad9c0ebb1bf4ce1 Mon Sep 17 00:00:00 2001 From: Unknown Date: Sun, 9 Jun 2013 20:18:11 +0800 Subject: [PATCH] 4.5.2.5.md --- README.md | 1 + eBook/04.5.md | 30 ++++++++++++++++++- eBook/exercises/chapter_4/count_characters.go | 22 ++++++++++++++ eBook/exercises/chapter_4/divby0.go | 8 +++++ .../chapter_4/function_calls_function.go | 18 +++++++++++ eBook/exercises/chapter_4/global_scope.go | 19 ++++++++++++ eBook/exercises/chapter_4/local_scope.go | 17 +++++++++++ 7 files changed, 114 insertions(+), 1 deletion(-) create mode 100644 eBook/exercises/chapter_4/count_characters.go create mode 100644 eBook/exercises/chapter_4/divby0.go create mode 100644 eBook/exercises/chapter_4/function_calls_function.go create mode 100644 eBook/exercises/chapter_4/global_scope.go create mode 100644 eBook/exercises/chapter_4/local_scope.go diff --git a/README.md b/README.md index bb282c8..9caa4ba 100644 --- a/README.md +++ b/README.md @@ -34,6 +34,7 @@ Golang中国:[golang.tc](http://www.golang.tc/) - 协助翻译: - [zhanming](https://github.com/zhanming) - [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 许可协议)。 diff --git a/eBook/04.5.md b/eBook/04.5.md index 5ffbf14..fd3a9c4 100644 --- a/eBook/04.5.md +++ b/eBook/04.5.md @@ -191,7 +191,7 @@ Example 4.9 [casting.go](examples/chapter_4/casting.go) 不过如果你实际存的数字超出你要转换到的类型的取值范围的话,则会引发 panic(第 13.2 节)。 -**问题 4.1: int 和 int64 是相同的类型吗?** +**问题 4.1** int 和 int64 是相同的类型吗? ###4.5.2.2 复数 Go 拥有以下复数类型: @@ -313,6 +313,34 @@ Go 中拥有以下逻辑运算符:`==`、`!=`(第 4.5.1 节)、`<`、`<=` b3:= 10 > 5 // b3 is true ###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 要不等到 ***2013 年 6 月 11 日*** 再来看看吧~~ diff --git a/eBook/exercises/chapter_4/count_characters.go b/eBook/exercises/chapter_4/count_characters.go new file mode 100644 index 0000000..8df046b --- /dev/null +++ b/eBook/exercises/chapter_4/count_characters.go @@ -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 +*/ \ No newline at end of file diff --git a/eBook/exercises/chapter_4/divby0.go b/eBook/exercises/chapter_4/divby0.go new file mode 100644 index 0000000..5a87c36 --- /dev/null +++ b/eBook/exercises/chapter_4/divby0.go @@ -0,0 +1,8 @@ +package main + +func main() { + a, b := 10, 0 + c := a / b // panic: runtime error: integer divide by zero + + print(c) +} diff --git a/eBook/exercises/chapter_4/function_calls_function.go b/eBook/exercises/chapter_4/function_calls_function.go new file mode 100644 index 0000000..f405dae --- /dev/null +++ b/eBook/exercises/chapter_4/function_calls_function.go @@ -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 \ No newline at end of file diff --git a/eBook/exercises/chapter_4/global_scope.go b/eBook/exercises/chapter_4/global_scope.go new file mode 100644 index 0000000..9973d90 --- /dev/null +++ b/eBook/exercises/chapter_4/global_scope.go @@ -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 \ No newline at end of file diff --git a/eBook/exercises/chapter_4/local_scope.go b/eBook/exercises/chapter_4/local_scope.go new file mode 100644 index 0000000..4f8bc03 --- /dev/null +++ b/eBook/exercises/chapter_4/local_scope.go @@ -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 \ No newline at end of file