I am trying to replace multiple different characters from a string using Replacer
but having issues replacing one string. Output has two underscores instead of one, and if I try replacing using other Replacer
, then it cannot replace it entirely.
Try the code on the Go Playground:
package main
import (
"fmt"
"strings"
)
//Expecting output to be emp_my_stats
func main() {
var input string = "/v1.0/emp/emp_1/my_stats"
replacer := strings.NewReplacer("/v1.0/", "", "/", "_", "emp_1", "")
// replacer := strings.NewReplacer("/v1.0/", "", "/", "_", "/emp_1", "")
output := replacer.Replace(input)
fmt.Printf("output %v", output)
}
I can use multiple Replacer
etc. but would really like to do it in one pass / or one statement.
Any suggestions how so do it cleanly? My goal is to be efficient (this will be done frequently, so important although these strings are short) and also to not use multiple Replacer
s.