Golang bytes.Map() function example
package bytes
Map returns a copy of the input byte slice with all its characters modified according to the mapping function. If mapping returns a negative value, the character is dropped from the string with no replacement. The characters in the input slice and the output are interpreted as UTF-8-encoded Unicode code points.
Golang bytes.Map() function usage example
package main
import (
"bytes"
"fmt"
)
func main() {
str := []byte("abcxefg")
mapping := func(replacekey rune) rune {
if replacekey == 'x' {
return 'd'
}
return replacekey
}
fmt.Println(string(str))
// now replace x with d
fmt.Println(string(bytes.Map(mapping, str)))
}
Output :
abcxefg
abcdefg
Reference :
Advertisement
Something interesting
Tutorials
+10k Golang : Function wrapper that takes arguments and return result example
+9.5k Golang : Timeout example
+4.2k Javascript : Empty an array example
+20.3k Golang : Convert seconds to human readable time format example
+8.3k Golang : Qt splash screen with delay example
+18k Golang : Qt image viewer example
+16.6k Golang : Execute terminal command to remote machine example
+5.3k PHP : Hide PHP version information from curl
+10.1k Golang : Read file and convert content to string
+11.7k Golang : Concurrency and goroutine example
+15.3k Golang : Save(pipe) HTTP response into a file
+18.8k Unmarshal/Load CSV record into struct in Go