Creating strings
In this recipe, we will look at how to create strings in a program.
How to do it...
- Use a string literal. There are two types of string literals in Go:
- Use interpreted string literals, between the double quotations:
x := "Hello world"
- With interpreted string literals, you must escape certain characters:
x:="This is how you can include a \" in your string literal" y:="You can also use a newline \n, tab \t"
- You can include Unicode codepoints or hexadecimal bytes, escaped with
'\'
:w:="\u65e5本\U00008a9e" x:="\xff"
- Use interpreted string literals, between the double quotations:
You cannot have newlines or an unescaped double-quote in an interpreted string:
- Use raw string literals, using backticks. A raw string literal can include any characters (including newlines) except a backtick. There is no way to escape backticks in a raw literal.
x:=`This is a multiline raw string literal. Backslash will print as backslash \`
If you need to include...