Remove Punctuation From a String in Go

Learn how to remove punctuation from a string in Go using strings.Map, unicode.IsPunct, custom predicates, and regexp with practical examples.

Before you start

This task runs entirely in your local Go program. You need Go installed and a small program or test file where you can edit and run code. No external service, camera, microphone, or network connection is required.

For most cases, use the standard library only. The strings, unicode, and regexp packages are available without downloading dependencies. The examples in this article use Go 1.20 or newer, but the relevant functions have been stable across many earlier versions.

Step-by-step instructions

Remove punctuation with strings.Map

The simplest approach is to build a new string by keeping only runes that are not punctuation. Use strings.Map with a function that returns -1 for punctuation runes and the rune unchanged for everything else.

package main

import (
	"fmt"
	"strings"
	"unicode"
)

func main() {
	input := "Hello, world! How are you?"
	cleaned := strings.Map(func(r rune) rune {
		if unicode.IsPunct(r) {
			return -1
		}
		return r
	}, input)

	fmt.Println(cleaned)
}

The output is "Hello world How are you". strings.Map treats a returned value of -1 as a request to drop that rune. This makes the predicate readable and avoids building a separate filtering loop.

Remove punctuation with regexp

If you want to declare the character classes explicitly, compile a regular expression with regexp.MustCompile and replace matches with an empty string. This is useful when the definition of punctuation differs from unicode.IsPunct.

package main

import (
	"fmt"
	"regexp"
)

func main() {
	input := "Hello, world! How are you?"
	re := regexp.MustCompile(`[[:punct:]]`)
	cleaned := re.ReplaceAllString(input, "")

	fmt.Println(cleaned)
}

The POSIX class [ :punct: ] removes characters classified as punctuation by the regular expression engine. Use regexp.Compile instead of regexp.MustCompile when the pattern comes from user input or configuration and may be invalid.

Remove a custom set of characters

For a known set such as commas, periods, exclamation marks, and question marks, use strings.Map with a switch or a strings.Contains check. This gives you exact control over which characters are removed.

package main

import (
	"fmt"
	"strings"
)

func main() {
	input := "Hello, world! How are you?"
	toRemove := ",.?!"
	cleaned := strings.Map(func(r rune) rune {
		if strings.ContainsRune(toRemove, r) {
			return -1
		}
		return r
	}, input)

	fmt.Println(cleaned)
}

This method is easier to review when only a few ASCII characters should be removed. It also avoids changing punctuation such as hyphens or slashes that unicode.IsPunct would classify as punctuation.

Check the output

  • Run the program and confirm that punctuation characters such as commas, periods, exclamation marks, and question marks are gone.
  • Check that spaces, letters, and digits remain in their original positions.
  • Verify that non-punctuation symbols you intended to keep, such as hyphens in filenames or underscores, are still present.
  • If the input contains Unicode punctuation, test a sample such as an em dash or curly quotes to confirm whether unicode.IsPunct removes it as expected.

The result is a new string, not a modification of the original. In Go, strings are immutable, so the input retains its original value unless you assign the cleaned result back to the same variable.

Common problems and next steps

A frequent issue is that punctuation disappears but adjacent spaces remain, producing double spaces or leading and trailing spaces. Apply strings.Fields or strings.TrimSpace afterward if you also need to normalize whitespace.

Another issue is removing more than intended. unicode.IsPunct classifies many symbols as punctuation, including characters that users may consider part of a word or filename. If that is not the desired behavior, switch to a custom set of characters.

When processing large amounts of text, avoid compiling a regular expression inside a loop. Compile it once and reuse it. For simple fixed-character removal, strings.Map is usually clearer and avoids relying on regexp semantics.

If the input comes from an audio transcript or OCR output, cleanup is only one step. You may still need to split lines, trim spaces, or remove other unwanted characters before the text is ready for display or further processing.