Transform Runes With strings.Map
Apply per-rune transformations with strings.Map: replace characters, drop runes by returning negative values, and see the result stay a fresh string.
Code
package main
import (
"fmt"
"strings"
"unicode"
)
func main() {
// 每个符文过一次映射函数:统一转大写以外的自定义规则
leet := strings.Map(func(r rune) rune {
switch r {
case 'a':
return '4'
case 'e':
return '3'
}
return r // 其余原样保留
}, "arena")
fmt.Println("mapped:", leet)
// 返回负值表示删除该符文:剔除空格
compact := strings.Map(func(r rune) rune {
if unicode.IsSpace(r) {
return -1
}
return r
}, "go pro tips")
fmt.Println("compact:", compact)
// 原字符串不可变:Map 总是产生新字符串
source := "arena"
fmt.Println("source unchanged:", source)
}Inside the snippet
Transform Runes With strings.Map is built around the function main.
Putting it to use
The demo output is labeled (mapped:, compact:, source unchanged:), so every line in the printed output names the step that produced it.
Core lines
return r // 其余原样保留
Interactive Tools for This Topic
ASCII Table ReferenceLook up any ASCII code point 0-127 by decimal, hex, or character and see the binary, octal, and control-name view.Character Frequency AnalyzerCount how often each character appears in a text, with Unicode code points, optional case folding, and whitespace exclusion.cURL to Python & Go ConverterTranslate curl CLI commands with headers, auth, JSON bodies, and timeouts into ready-to-run Python requests and Go net/http client code snippets.SRI Hash GeneratorGenerate Subresource Integrity (SRI) hashes from resource content using SHA-256, SHA-384, or SHA-512, and copy a ready-to-use script tag.
FAQ
- What does Transform Runes With strings.Map print when run?
- Running the file directly is enough — expect labeled output lines such as `mapped:`, `compact:`, `source unchanged:`.
Report a problem with this exampleOpens a pre-filled issue on GitHub.