I was wondering how to remove:
Use regexp for this.
func main() {
data := []byte(" Hello, World ! ")
re := regexp.MustCompile(" +")
replaced := re.ReplaceAll(bytes.TrimSpace(data), []byte(" "))
fmt.Println(string(replaced))
// Hello, World !
}
In order to also trim newlines and null characters, you can use the bytes.Trim(src []byte, cutset string)
function instead of bytes.TrimSpace