Golang remove backslash from string

Remove backslash from a string in Go with strings.ReplaceAll or strings.Replace, including raw literals and optional cleanup. Related: golang remove substring and golang remove characters.

Published

Updated

Read time 3 min read

Reviewed byDeepak Prasad

Golang remove backslash from string

If you want to remove backslash from string values in Go, the usual answer is strings.ReplaceAll or strings.Replace from the standard library. Searches like golang strings ReplaceAll, strings replaceall golang, or golang strings replaceall all describe the same helpers. This page shows how to pick the old argument correctly for raw versus interpreted string literals, how to clean up extra spaces, and where bash remove backslash from string fits if you are in the shell instead of Go. Queries about other languages (for example Java) are out of scope here; the same idea exists there but with different APIs.

Examples were checked with a recent Go toolchain on Linux. The shell one-liner was run in bash.


Remove backslashes with strings.Replace and strings.ReplaceAll

strings.Replace(s, old, new, n) returns a copy of s with up to n non-overlapping copies of old replaced by new. If n < 0, there is no limit, which is the same behavior as strings.ReplaceAll(s, old, new) for typical uses. For golang remove substring style edits where every backslash must go, ReplaceAll is enough.

If old is empty, the behavior is special (matches between UTF-8 sequences); that corner case is documented in strings.Replace. For removing a literal backslash, old is never empty.

One program: raw strings, double-quoted strings, and cleanup

This program uses a raw string for str1 so each \ in the text is one rune. For str2 the source uses "\\" pairs so the string still contains single backslash runes at runtime. Run prints the before/after lines so you can confirm spaces.

go
package main

import (
	"fmt"
	"strings"
)

func main() {
	str1 := `Welcome \ to GoLinuxCloud \ Nice to meet you!`
	str2 := "Today is Sunday \\ Have a nice weekend\\!"

	fmt.Println("str1:", str1)
	fmt.Println("str2:", str2)

	r1 := strings.ReplaceAll(str1, `\`, "")
	r2 := strings.ReplaceAll(str2, `\`, "")
	fmt.Println("after ReplaceAll (spaces may double):", r1)
	fmt.Println("after ReplaceAll str2:", r2)

	r1tidy := strings.ReplaceAll(strings.ReplaceAll(str1, `\`, ""), "  ", " ")
	r2tidy := strings.ReplaceAll(strings.ReplaceAll(str2, `\`, ""), "  ", " ")
	fmt.Println("after tidy:", r1tidy)
	fmt.Println("after tidy str2:", r2tidy)
}
Output

Run shows doubled spaces right after ReplaceAll, then single spaces once the second ReplaceAll collapses " " to " ". Adjust that cleanup if your text uses tabs or other separators.

For more on how backslashes appear inside literals, see the companion note on escaping backslashes in Go. If you need broader trimming after edits, see trimming strings.

Bash remove backslash from string

In bash you often pipe text through tr or sed. Here printf keeps backslashes literal in single quotes:

bash
printf '%s\n' 'a\b\c' | tr -d '\\'
text
abc

That is only a shell hint; in Go code prefer strings.ReplaceAll for clarity and UTF-8 safety on full strings.


Summary

Removing backslashes in Go is usually a single strings.ReplaceAll call: set old to one backslash rune—written \ inside raw literals or \\ inside double-quoted code—and set new to empty. strings.Replace with n == -1 matches that behavior when you do not need a partial count. After removal, watch for doubled spaces where a backslash used to sit; a second replace pass or trimming can fix layout without changing the core API. For shell-only workflows, bash remove backslash from string one-liners such as tr can strip the character in a pipeline; inside Go programs the strings API stays the clearer choice.


References


Frequently Asked Questions

1. Should I use strings.Replace or strings.ReplaceAll?

Prefer strings.ReplaceAll when you want every occurrence replaced; it reads clearly and matches most remove backslash from string tasks. Use strings.Replace when you need a finite count via the n argument.

2. Why do I see "\\" in one example and a raw `\` in another?

In a double-quoted Go string, you write a literal backslash as "\". In a raw string literal using backticks, a single backtick cannot appear unescaped, but a single backslash is written as one backslash character inside the backticks.

3. After removing backslashes, why are there double spaces?

If the original text had spaces around each backslash, removing the backslash leaves two spaces. Collapse them with another Replace or ReplaceAll on " " to " ", or trim fields depending on your layout.

4. Does ReplaceAll treat backslash specially?

No. Backslash is a normal rune in the string; only string and regexp literal syntax in source code makes it look special.
Tuan Nguyen

Data Scientist

Proficient in Golang, Python, Java, MongoDB, Selenium, Spring Boot, Kubernetes, Scrapy, API development, Docker, Data Scraping, PrimeFaces, Linux, Data Structures, and Data Mining. With expertise …