O'Reilly logo

Go Standard Library Cookbook by Radomir Sohlich

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

How it works...

The Replace function of a strings package is widely used for simple replacement. The last integer argument defines how many replacements will be done (in case of -1, all strings are replaced. See the second use of Replace, where only the first two occurrences are replaced.) The use of the Replace function is presented in steps 1 - 5.

Besides the Replace function, the Replacer structure also has the WriteString method. This method will write to the given writer with all replacements defined in Replacer The main purpose of this type is its reusability. It can replace multiple strings at once and it is safe for concurrent use; see steps 6 - 8.

The more sophisticated method of replacing the substring, or even the matched pattern, ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required