Golang: String Backslash Escape

By Xah Lee. Date: .

Within a double quoted string, character sequence starting with backslash may have special meaning. e.g. \n means newline.

package main

import "fmt"

func main() {
    var x = "a\nb"
    fmt.Println(x)
}

// prints
// a
// b

Here's complete list:

[see ASCII Characters ␀ ␣ ¶]

package main

import "fmt"

func main() {
    fmt.Printf("%v\n", "A" == "\x41")       // true
    fmt.Printf("%v\n", "♥" == "\u2665")     // true
    fmt.Printf("%v\n", "😂" == "\U0001f602") // true
}

Reference

The Go Programming Language Specification - The Go Programming Language#String_literals

If you have a question, put $5 at patreon and message me.

Golang

  1. Compile, Run
  2. Source Encoding
  3. Package, Import
  4. Comment
  5. Print
  6. String
  7. String Backslash Escape
  8. Rune
  9. Variable
  10. Zero Value
  11. Constant
  12. If Then Else
  13. Switch/Case
  14. Loop
  15. Basic Types
  16. Array
  17. Slice
  18. Map
  19. Struct
  20. Function
  21. String as Chars
  22. regexp
  23. Read File
  24. Write to File
  25. Walk Dir
  26. Check File Exist
  27. System Call
  28. Get Script Path
  29. Pointer
  30. Defer
  31. Random Number

Examples

  1. match any regexp
  2. Validate Links
  3. Generate Sitemap

Reference

  1. Go Spec