How to remove symbols from a string in Golang?

Remove symbols from a given string
In the following program ReplaceAllString() method is used, which allows us to replace original string with another string if the specified string matches with the specified regular expression. This method is defined under the regexp package, hence to access ReplaceAllString() method first we need to import the regexp package in our program.


package main

import (

func main() {
	str1 := "how much for the maple syrup? $20.99? That's ridiculous!!!"

	re, err := regexp.Compile(`[^\w]`)
	if err != nil {
	str1 = re.ReplaceAllString(str1, " ")
In the above program, we have replaced special characters with white-space.


how much for the maple syrup   20 99  That s ridiculous

Most Helpful This Week