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
+14k Golang : Reverse IP address for reverse DNS lookup example
+16.6k Golang : Generate QR codes for Google Authenticator App and fix "Cannot interpret QR code" error
+7.8k Golang : Regular Expression find string example
+17.9k Golang : Simple client server example
+26.9k Golang : Force your program to run with root permissions
+8.2k Golang : Get final or effective URL with Request.URL example
+12.8k Golang : http.Get example
+5.3k Golang : Pad file extension automagically
+12.7k Golang : Remove or trim extra comma from CSV
+14.9k Golang : How to check for empty array string or string?
+25.6k Golang : convert rune to integer value