mirror of
https://github.com/unknwon/the-way-to-go_ZH_CN.git
synced 2025-11-13 09:16:10 +08:00
@@ -24,11 +24,11 @@ func callback(y int, f func(int, int)) {
|
||||
|
||||
输出:
|
||||
|
||||
The sum of 1 and 2 is: 3
|
||||
The sum of 1 and 2 is: 3
|
||||
|
||||
将函数作为参数的最好的例子是函数 `strings.IndexFunc()`:
|
||||
|
||||
该函数的签名是 `func IndexFunc(s string, f func(c rune) bool) int`,它的返回值是字符串s中第一个使函数`f(c)`返回`true`的Unicode字符的索引值。如果找不到,则返回-1。
|
||||
该函数的签名是 `func IndexFunc(s string, f func(c rune) bool) int`,它的返回值是字符串 s 中第一个使函数 `f(c)` 返回 `true` 的 Unicode 字符的索引值。如果找不到,则返回 -1。
|
||||
|
||||
例如 `strings.IndexFunc(line, unicode.IsSpace)` 就会返回 `line` 中第一个空白字符的索引值。当然,您也可以书写自己的函数:
|
||||
|
||||
@@ -41,16 +41,16 @@ func IsAscii(c int) bool {
|
||||
}
|
||||
```
|
||||
|
||||
在第 14.10.1 节中,我们将会根据一个客户端/服务端程序作为示例对这个用法进行深入讨论。
|
||||
在[第 14.10.1 节](14.10.md) 中,我们将会根据一个客户端/服务端程序作为示例对这个用法进行深入讨论。
|
||||
|
||||
```go
|
||||
type binOp func(a, b int) int
|
||||
func run(op binOp, req *Request) { … }
|
||||
```
|
||||
|
||||
**练习 6.7**
|
||||
**练习 6.7** [strings_map.go](exercises\chapter_6\strings_map.go)
|
||||
|
||||
包 `strings` 中的 `Map` 函数和 `strings.IndexFunc()` 一样都是非常好的使用例子。请学习它的源代码并基于该函数书写一个程序,要求将指定文本内的所有非 ASCII 字符替换成 `?` 或空格。您需要怎么做才能删除这些字符呢?
|
||||
包 `strings` 中的 `Map()` 函数和 `strings.IndexFunc()` 一样都是非常好的使用例子。请学习它的源代码并基于该函数书写一个程序,要求将指定文本内的所有非 ASCII 字符替换成问号 `'?'` 或空格 `' '`。您需要怎么做才能删除这些字符呢?
|
||||
|
||||
## 链接
|
||||
|
||||
|
||||
Reference in New Issue
Block a user