To replace all occurrences of a substring within a string in Go, you can use the strings.Replace()
function. It takes four arguments:
Here's an example:
main.go214 chars13 lines
This will output:
main.go44 chars2 lines
Note that the Replace()
function returns a new string with the replacements made - it does not modify the original string. If you want to modify the original string, you can do something like:
main.go270 chars14 lines
This will output:
main.go44 chars2 lines
gistlibby LogSnag