I have a function in which I need to replace "byte" with "$ball". This doesn't seem to work correctly. Here is the program snippet.
fun main() {
str := []byte("$apple in a byte
")
strReplace := "$ball"
re := regexp.MustCompile("byte")
final := re.ReplaceAll(str, []byte(strReplace))
ioutil.WriteFile("testfile.txt", final, 0744)
}
Expected Output in testfile.txt: $apple in a $ball
Actual Output in testfile.txt: $apple in a
Any solutions for successfully getting the desired output?
You use $$
which is exactly what the documentation tells you.
The godoc section for ReplaceAll
(https://godoc.org/regexp#Regexp.ReplaceAll) tells you:
$ signs are interpreted as in Expand
So reading the section for Expand
has the answer. https://godoc.org/regexp#Regexp.Expand
Please throughly read all relevant documentation for the package and functions you are using before posting a question.