GO語言"strings"包中"Map"函數的用法及代碼示例。
用法:
func Map(mapping func(rune) rune, s string) string
Map 返回字符串 s 的副本,其中所有字符都根據映射函數進行了修改。如果映射返回負值,則從字符串中刪除該字符而不進行替換。
例子:
package main
import (
"fmt"
"strings"
)
func main() {
rot13 := func(r rune) rune {
switch {
case r >= 'A' && r <= 'Z':
return 'A' + (r-'A'+13)%26
case r >= 'a' && r <= 'z':
return 'a' + (r-'a'+13)%26
}
return r
}
fmt.Println(strings.Map(rot13, "'Twas brillig and the slithy gopher..."))
}
輸出:
'Gjnf oevyyvt naq gur fyvgul tbcure...
相關用法
- GO Map用法及代碼示例
- GO MakeFunc用法及代碼示例
- GO MakeTable用法及代碼示例
- GO MarshalIndent用法及代碼示例
- GO Marshal用法及代碼示例
- GO MatchString用法及代碼示例
- GO Match用法及代碼示例
- GO MethodSet用法及代碼示例
- GO Mul32用法及代碼示例
- GO Mkdir用法及代碼示例
- GO MkdirTemp用法及代碼示例
- GO Modf用法及代碼示例
- GO Mul64用法及代碼示例
- GO Mod用法及代碼示例
- GO MultiReader用法及代碼示例
- GO MultiWriter用法及代碼示例
- GO Month用法及代碼示例
- GO MkdirAll用法及代碼示例
- GO PutUvarint用法及代碼示例
- GO Scanner.Scan用法及代碼示例
注:本文由純淨天空篩選整理自golang.google.cn大神的英文原創作品 Map。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。