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) *Replacer

Parameters & Attributes

NameTypeDescription
oldnew...stringAlternating 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>"))}
Go Reference