In this section, we're going to see how to escape special characters in string values. Similar to many other languages in the market today, Go treats certain characters in a special way. For instance, if Go sees \t characters in a string value, it will treat them as a tab character instead. Also, without escaping it, you cannot have double quotes within a double quote, and right now we're going to see how to escape them in order to properly show those characters to our output.
As always, we will have our main.go
file and the main
function. So let's check an example that is similar to the previous one.
package main import "fmt" func main(){ helloWorld := "Hello World, this is Tarik." }
So, if I want to include double quotes around the term Tarik, I can do it, but, as you can see, it gives me a compile time error, as shown in the following screenshot:
So, let's fix this. All I need to do is use \
. So, anytime you want to escape a special character, you escape...