GO語言"bytes"包中"Map"函數的用法及代碼示例。
用法:
func Map(mapping func(r rune) rune, s []byte) []byte
Map 返回字節切片 s 的副本,其中所有字符都根據映射函數進行了修改。如果映射返回負值,則從字節片中刪除該字符而不進行替換。 s 和輸出中的字符被解釋為 UTF-8 編碼的代碼點。
例子:
package main
import (
"bytes"
"fmt"
)
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.Printf("%s", bytes.Map(rot13, []byte("'Twas brillig and the slithy gopher...")))
}
輸出:
Her Royal Highness
相關用法
- 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用法及代碼示例
- GO LeadingZeros32用法及代碼示例
注:本文由純淨天空篩選整理自golang.google.cn大神的英文原創作品 Map。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。