I am programming in Go and I read a text file in and I replace multiple things on it to translate the code from one language to Go to be able to run. The problem I am having is that when trying to replace things like Println statements I cannot get a parenthesis on the end of the statement without being really specific to the code I am converting. Is there a way to use the code like this?
src = bytes.Replace(src, []byte("Insert"), []byte("Println(" * ")"), -1)
and have the ability to just put a parenthesis at the end of the line of code?
package main
import (
"fmt"
"regexp"
)
func main() {
src := []byte(`
Write(1, 3, "foo", 3*qux(42));
WriteLn("Enter bar: ");
`)
re := regexp.MustCompile(`Write\((.*)\);`)
re2 := regexp.MustCompile(`WriteLn\((.*)\);`)
src = re.ReplaceAll(src, []byte(`Print($1)`))
src = re2.ReplaceAll(src, []byte(`PrintLn($1)`))
fmt.Printf("%s", src)
}
(Alse here)
Output:
Print(1, 3, "foo", 3*qux(42))
PrintLn("Enter bar: ")