remove a character from a string in go

To remove a single character from a Go string, you can use the strings.Replace() function in combination with an empty string for the replacement argument.

Here's an example that removes the character 'o' from the string "foo bar":

main.go
package main

import (
	"fmt"
	"strings"
)

func main() {
	str := "foo bar"
	newStr := strings.Replace(str, "o", "", -1)
	fmt.Println(newStr) // "f bar"
}
155 chars
13 lines

In the example above, strings.Replace() takes three arguments: the original string (str), the character to be replaced ("o"), and an empty string as the replacement. -1 is passed in as the n argument to replace all occurrences of the character.

The resulting string with the removed character is stored in newStr. We then print out newStr, which should be "f bar" in this case.

gistlibby LogSnag