mirror of
https://github.com/unknwon/the-way-to-go_ZH_CN.git
synced 2025-08-12 03:06:41 +08:00
Update 07.3.md and 07.6.md (#273)
* Update 07.3.md * Update 07.3.md * Update 07.6.md
This commit is contained in:
@@ -67,7 +67,7 @@ for ix := range seasons {
|
||||
|
||||
```go
|
||||
for row := range screen {
|
||||
for column := range screen[0] {
|
||||
for column := range screen[row] {
|
||||
screen[row][column] = 1
|
||||
}
|
||||
}
|
||||
@@ -89,7 +89,7 @@ b) 如果 a) 无法正常工作,写一个 for 循环让值可以 double。
|
||||
|
||||
**练习 7.7** sum_array.go
|
||||
|
||||
a) 写一个 Sum 函数,传入参数为一个 4 位 float 数组成的数组 arrF,返回该数组的所有数字和。
|
||||
a) 写一个 Sum 函数,传入参数为一个 32 位 float 数组成的数组 arrF,返回该数组的所有数字和。
|
||||
|
||||
如果把数组修改为切片的话代码要做怎样的修改?如果用切片形式方法实现不同长度数组的的和呢?
|
||||
|
||||
|
@@ -23,9 +23,9 @@ func main() {
|
||||
|
||||
0:ÿ 2:界
|
||||
|
||||
我们知道,Unicode 字符会占用 2 个字节,有些甚至需要 3 个或者 4 个字节来进行表示。如果发现错误的 UTF8 字符,则该字符会被设置为 U+FFFD 并且索引向前移动一个字节。和字符串转换一样,您同样可以使用 `c := []int(s)` 语法,这样切片中的每个 int 都会包含对应的 Unicode 代码,因为字符串中的每次字符都会对应一个整数。类似的,您也可以将字符串转换为元素类型为 rune 的切片:`r := []rune(s)`。
|
||||
我们知道,Unicode 字符会占用 2 个字节,有些甚至需要 3 个或者 4 个字节来进行表示。如果发现错误的 UTF8 字符,则该字符会被设置为 U+FFFD 并且索引向前移动一个字节。和字符串转换一样,您同样可以使用 `c := []int32(s)` 语法,这样切片中的每个 int 都会包含对应的 Unicode 代码,因为字符串中的每次字符都会对应一个整数。类似的,您也可以将字符串转换为元素类型为 rune 的切片:`r := []rune(s)`。
|
||||
|
||||
可以通过代码 `len([]int(s))` 来获得字符串中字符的数量,但使用 `utf8.RuneCountInString(s)` 效率会更高一点。(参考[count_characters.go](exercises/chapter_4/count_characters.go))
|
||||
可以通过代码 `len([]int32(s))` 来获得字符串中字符的数量,但使用 `utf8.RuneCountInString(s)` 效率会更高一点。(参考[count_characters.go](exercises/chapter_4/count_characters.go))
|
||||
|
||||
您还可以将一个字符串追加到某一个字符数组的尾部:
|
||||
|
||||
@@ -178,7 +178,7 @@ func FindDigits(filename string) []byte {
|
||||
|
||||
如果您使用两个切片来实现反转,请再尝试使用一个切片(提示:使用交换法)。
|
||||
|
||||
如果您想要反转 Unicode 编码的字符串,请使用 `[]int` 类型的切片。
|
||||
如果您想要反转 Unicode 编码的字符串,请使用 `[]int32` 类型的切片。
|
||||
|
||||
**练习 7.15**
|
||||
|
||||
|
@@ -7,7 +7,7 @@ import (
|
||||
)
|
||||
|
||||
func main() {
|
||||
sl1 := []int{78,34,643,12,90,492, 13, 2}
|
||||
sl1 := []int{78, 34, 643, 12, 90, 492, 13, 2}
|
||||
max := maxSlice(sl1)
|
||||
fmt.Printf("The maximum is %d\n", max)
|
||||
min := minSlice(sl1)
|
||||
@@ -23,7 +23,7 @@ func maxSlice(sl []int) (max int) {
|
||||
return
|
||||
}
|
||||
|
||||
func minSlice(sl [] int) (min int) {
|
||||
func minSlice(sl []int) (min int) {
|
||||
// min = int(^uint(0) >> 1)
|
||||
min = math.MaxInt32
|
||||
for _, v := range sl {
|
||||
@@ -33,6 +33,7 @@ func minSlice(sl [] int) (min int) {
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
/* Output:
|
||||
The maximum is 643
|
||||
The minimum is 2
|
||||
|
Reference in New Issue
Block a user