Go string replacement with replace function

You can replace a character, a word or even a string from a string through replace function of string package in Go. replace function required 4 string parameter to make replacement on the string first parameter is whole string, second parameter old string which need to be replaced, third parameter is new string with you want to replace old string and fourth param the number of replacements (use -1 to replace all). The example of replace method is mentioned below: Code


package main

import (
    "fmt"
    "strings"
)

func main() {
    // Declare a string variable
    greetingStr := "Good Morning"

    // Replace substring
    replacedStr := strings.Replace(greeting, "Morning", "Evening", 1)
    fmt.Println("After replace:", replacedStr)
}
In above mentioned example we have a string variable with the name of greetingStr and we make a replacement with replace method, we replace 'Morning' with 'Evening'.