String Replacements in Go

Software Engineer with an entrepreneurship gene. Love talking about code, products, UI/UX and how to build startups.
Search for a command to run...

Software Engineer with an entrepreneurship gene. Love talking about code, products, UI/UX and how to build startups.
No comments yet. Be the first to comment.
Goroutines are one of the best features of Go language. It is very easy to run a function as a goroutine, you just have to use the keyword go before the function call. Here is a very simple function which sleeps for n seconds and prints a line. pa...

Go is a modern language, but doesn't use try-except blocks to handle errors. Errors are simple values that can be returned from functions and it is common to check for errors before proceeding with your next steps. Let's see the different ways to cre...

In the previous post, we saw how to write a simple unit test case and also how to write table driven test cases. In this post we will quickly see how to write benchmarks, as go supports running benchmarks on your functions as part of the standard tes...
Go has built-in support for writing test cases for your code. And it is important to write as much test cases as possible to make sure you cover all possible conditions. You have to use the testing standard package to write your unit test cases. Let...
Pro Golang Dev
14 posts
In a previous post, we saw how we can use Regular expressions in Go to match and replace patterns in a string. While regexps are very useful, you might not want to them for every time you want to do some basic string manipulation. The strings package has few functions which works well for basic string replacement.
Let's look at two such functions.
Just as the name suggests, this strings.Replace function does a very basic string replacement. This is the signature of this function.
func Replace(s, old, new string, n int) string
s is the string in which you want to replace old with new and the integer n tells how many occurences should be replaced. If n is < 0, all occurences will be replaced.
package main
import (
"strings"
"fmt"
)
const refString = "Apple is the most customer centric company. Apple is the first company to reach 1 Trillion market cap in the world."
func main() {
out := strings.Replace(refString, "Apple", "Amazon", 1)
fmt.Println(out)
}
As expected, this prints the following.
Amazon is the most customer centric company. Apple is the first company to reach 1 Trillion market cap in the world.
The strings.Replace works fine if you have only one string to replace. If you have multiple strings, you would need something more powerful. Thats why we have the strings.NewReplacer. The NewReplacer function takes in pairs of old:new strings and returns a new Replacer. You can call the Replace method on this struct to do a quick replacement on your string.
package main
import (
"fmt"
"strings"
)
func main() {
r := strings.NewReplacer("<", "<", ">", ">")
fmt.Println(r.Replace("This is <b>HTML</b>!"))
}
The advantage of using the strings.NewReplacer is that it uses an efficient data structure (called Trie) to do all the replacements in a single pass of the string. This is much faster if you have a lot of very basic string replacements.
However if you need to use various patterns to match, you are stuct with constructing and compiling your regular expression.