Standard library / strings
strings.NewReplacer()
Creates a reusable replacer from old/new pairs. Odd argument count panics; matches are non-overlapping and pair order decides competing matches. Safe for concurrent use. Expected stdout: '[go]\n'.
Formal Syntax & Structure
go syntaxgo
func NewReplacer(oldnew ...string) *ReplacerParameters & Attributes
| Name | Type | Description |
|---|---|---|
| oldnew | ...string | Alternating old and replacement strings. |
Return Value
*Replacer
Creates a reusable replacer from old/new pairs. Odd argument count panics; matches are non-overlapping and pair order decides competing matches. Safe for concurrent use.
Working Example
Example Snippetgo
package main
import ( "fmt" "strings")
func main() { r := strings.NewReplacer("<", "[", ">", "]") fmt.Println(r.Replace("<go>"))}