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
+9.5k Golang : Changing a RGBA image number of channels with OpenCV
+12.8k Golang : http.Get example
+18.5k Golang : Aligning strings to right, left and center with fill example
+26k Golang : Convert IP address string to long ( unsigned 32-bit integer )
+9.4k Golang : Web(Javascript) to server-side websocket example
+9.7k Random number generation with crypto/rand in Go
+9.4k Golang : Detect Pascal, Kebab, Screaming Snake and Camel cases
+17.2k Google Chrome : Your connection to website is encrypted with obsolete cryptography
+10.1k Golang : How to tokenize source code with text/scanner package?
+14.6k Golang : Send email with attachment(RFC2822) using Gmail API example
+11.2k Golang : How to pipe input data to executing child process?