Escaping '.' character in string
Escaping '.' character in string
I am trying to escape all occurrence of '.' character in a string with backslash. When I run my Go code it works fine but when I compile and build the binary and then run it, it adds one more backslash.
s := "strings.test"
i := strings.Replace(s, ".", "\.", -1)
I even tried
s := "strings.test"
i := strings.Replace(s, ".", `.`, -1)
Output: "strings\.test"
Expected Output: "strings.test"
Not sure what is the difference between running the main.go vs building the project and then running it.
\\.
1 Answer
1
Are you sure about your output? This Go Playground gives me your desired output https://play.golang.org/p/fGzLeE_3h6h
Compiling does the same, I get the correct output... Are you sure your code is running fine (with the values you expect)?
go run
does the same as go build
however go run
executes the compiled binary for you, more like a shortcut
go run
go build
go run
Yeah, that's what is strange about this. One thing, I am running in my mac but build and test is run on a RHEL server. Could this issue be because of OS?
– ravi
Aug 22 at 0:12
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
The output should be same and is same for both building and running the file..The output you are saying should be when you chose new string as
\\.
.– Hamza Anis
Aug 22 at 0:06