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
+32.8k Golang : How to stream file to client(browser) or write to http.ResponseWriter?
+6.1k Golang : Validate credit card example
+4k Golang : Check if a word is countable or not
+37.6k Golang : How to read CSV file
+16.9k Golang : Get download file size
+6.1k Nginx : How to block user agent ?
+8.9k Golang : Setting variable value with ldflags
+5.5k Unix/Linux : Use netstat to find out IP addresses served by your website server
+6.4k Golang : Example of how to detect which type of script a word belongs to
+5.2k Golang : Join lines with certain suffix symbol example
+4.7k Javascript : How to refresh page with JQuery ?
+5.9k Golang : Decode XML data from RSS feed