How do you write multiline strings in Go?

前端 未结 9 916
傲寒
傲寒 2020-12-07 07:25

Does Go have anything similar to Python\'s multiline strings:

\"\"\"line 1
line 2
line 3\"\"\"

相关标签:
9条回答
  • 2020-12-07 07:49

    From String literals:

    • raw string literal supports multiline (but escaped characters aren't interpreted)
    • interpreted string literal interpret escaped characters, like '\n'.

    But, if your multi-line string has to include a backquote (`), then you will have to use an interpreted string literal:

    `line one
      line two ` +
    "`" + `line three
    line four`
    

    You cannot directly put a backquote (`) in a raw string literal (``xx\).
    You have to use (as explained in "how to put a backquote in a backquoted string?"):

     + "`" + ...
    
    0 讨论(0)
  • 2020-12-07 07:56

    you can use raw literals. Example

    s:=`stack
    overflow`
    
    0 讨论(0)
  • 2020-12-07 08:01

    For me this is what I use if adding \n is not a problem.

    fmt.Sprintf("Hello World\nHow are you doing today\nHope all is well with your go\nAnd code")
    

    Else you can use the raw string

    multiline := `Hello Brothers and sisters of the Code
                  The grail needs us.
                 `
    
    0 讨论(0)
提交回复
热议问题