Welcome To Golang By Example

Remove or Strip all white spaces from a string in Go (Golang)

strings.ReplaceAll function can be used to trim all white spaces from a string in Golang. Below is the signature of the function

func ReplaceAll(s, old, new string) string

Working Code:

package main

import (
    "fmt"
    "strings"
)

func main() {
    sample := " This is a sample string   "
    noSpaceString := strings.ReplaceAll(sample, " ", "")
    fmt.Println(noSpaceString)
}

Output:

Thisisasamplestring