Goで複数行の文字列を記述するにはどうすればいいですか?


How do you write multiline strings in Go?
Does Go have anything similar to Python's multiline strings:GoはPythonの複数行文字列に似ていますか?
"""line 1
line 2
line 3"""

If not, what is the preferred way of writing strings spanning multiple lines? そうでない場合、複数行にまたがる文字列を作成する最初の方法は何ですか?
1階
参照先:https://stackoom.com/question/XHr2/Goで複数行文字列を記述する方法
2階
From String literals:文字列から:
  • raw string literal supports multiline(but escaped characters aren't interpreted)元の文字列文字は複数行をサポートする(ただし、エスケープ文字は説明しない)
  • interpreted string literal interpret escaped characters, like ' \
    '. 解釈された文字列文字は、「\
    」などのエスケープされた文字を解釈する.

  • 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 \\ ). 元の文字列(`xx\\)に逆引用符(`)を直接追加することはできません.You have to use (as explained in "how to put a backquote in a backquoted string? "): (反引用符の文字列に反引用符を付ける方法など)を使用する必要があります.)を使用して、現在実行中の解析ジョブをすべて停止します.
     + "`" + ...
    

    #3階
    You can put content with``around it,likeコンテンツの周りに``を付けることができます
    var hi = `I am here,
    hello,
    `
    

    #4階
    Useraw string literals for multi-line strings:複数行の文字列に対して元の文字列を使用:
    func main(){
        multiline := `line 
    by line
    and line
    after line`
    }
    

    Raw string literalsオリジナル文字列文字
    Raw string literals are character sequences between back quotes, as in `foo` . 元の文字列は、`foo`などの逆引用符の文字列です.Within the quotes, any character may appear except back quote. 引用符では、逆引用符以外の文字が表示されます.
    A significant part is that is raw literal not just multi-line and to be multi-line is not the only purpose of it. 1つの重要な部分は、元の文字が複数行だけでなく、複数行になることが唯一の目的ではないことです.
    The value of a raw string literal is the string composed of the uninterpreted (implicitly UTF-8-encoded) characters between the quotes; 元の文字列の値は、引用符の間で説明されていない(UTF-8符号化されている)文字からなる文字列です.in particular, backslashes have no special meaning... 特にスラッシュには特別な意味はありません...
    So escapes will not be interpreted and new lines between ticks will be real new lines . したがって、エスケープ記号は解釈されず、目盛線間の新しい行が本当の新しい行になります.
    func main(){
        multiline := `line 
    by line 
    and line
    after line` //
    will be just printed. // But new lines are there too. fmt.Print(multiline) }

    Concatenationカスケード
    Possibly you have long line which you want to break and you don't need new lines in it. 長い行が中断され、新しい行は必要ない場合があります.In this case you could use string concatenation. この場合、文字列接続を使用できます.
    func main(){
        multiline := "line " +
                "by line " +
                "and line " +
                "after line"
    
        fmt.Print(multiline) // No new lines here
    }
    

    Since ""is interpreted string literal escapes will be interpreted. 「」は文字列として解釈されるため、字面量の変換が解釈される.
    func main(){
        multiline := "line " +
                "by line 
    " + "and line
    " + "after line" fmt.Print(multiline) // New lines as interpreted
    }

    #5階
    You have to be very careful on formatting and line spacing in go, everything counts and here is a working sample, try it https://play.golang.org/p/c0zeXKYlmFフォーマットと行間隔に注意する必要があります.すべてが重要です.これは有効な例です.試してみてください.https://play.golang.org/p/c0zeXKYlmF
    package main
    
    import "fmt"
    
    func main() {
        testLine := `This is a test line 1
    This is a test line 2`
        fmt.Println(testLine)
    }
    

    #6階
    you can use raw literals. 元のテキストを使用できます.Example例
    s:=`stack
    overflow`