How to Replace Part of a String in Go

Replace part of a string in Go using the strings.Replace function with the original string, target, replacement, and count.

Use the strings.Replace function to replace a specific substring with a new one. It returns a new string with the replacements made, leaving the original string unchanged.

import "strings"

original := "Hello, World!"
result := strings.Replace(original, "World", "Go", 1)
// result is "Hello, Go!"

The function takes the original string, the target substring, the replacement string, and the number of replacements to make (use -1 for all occurrences).