Programing

Go에서 여러 줄 문자열을 어떻게 작성합니까?

lottogame 2020. 10. 4. 10:15
반응형

Go에서 여러 줄 문자열을 어떻게 작성합니까?


Go에는 Python의 여러 줄 문자열과 유사한 것이 있습니까?

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

그렇지 않은 경우 여러 줄에 걸쳐 문자열을 쓰는 데 선호되는 방법은 무엇입니까?


에 따르면 언어 사양 당신은 문자열 대신 큰 따옴표의 역 따옴표로 구분되는 원시 문자열 리터럴을 사용할 수 있습니다.

`line 1
line 2
line 3`

당신은 쓸 수 있습니다:

"line 1" +
"line 2" +
"line 3"

다음과 동일합니다.

"line 1line 2line3"

백틱을 사용하는 것과 달리 이스케이프 문자를 유지합니다. "+"는 '선행'줄에 있어야합니다. 즉 :

"line 1"
+"line 2"

오류를 생성합니다.


에서 문자열 리터럴 :

  • 원시 문자열 리터럴은 여러 줄을 지원하지만 이스케이프 된 문자는 해석되지 않습니다.
  • 해석 된 문자열 리터럴 ' \n' 과 같이 이스케이프 된 문자를 해석합니다 .

그러나 여러 줄 문자열에 역 따옴표 (`)가 포함되어야하는 경우 해석 된 문자열 리터럴을 사용해야합니다.

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

원시 문자열 리터럴 (``xx \)에 역 따옴표 (`)를 직접 넣을 수 없습니다 .
다음을 사용해야합니다 ( " 역 따옴표 문자열에 역 따옴표를 넣는 방법? "에 설명 됨).

 + "`" + ...

여러 줄 문자열에 원시 문자열 리터럴을 사용합니다.

func main(){
    multiline := `line 
by line
and line
after line`
}

원시 문자열 리터럴

원시 문자열 리터럴은에서와 같이 역 따옴표 사이의 문자 시퀀스입니다 `foo`. 따옴표 안에는 역 따옴표를 제외한 모든 문자가 나타날 수 있습니다.

중요한 부분은 여러 줄이 아닌 원시 리터럴이며 여러 줄이되는 것이 유일한 목적이 아니라는 것입니다.

The value of a raw string literal is the string composed of the uninterpreted (implicitly UTF-8-encoded) characters between the quotes; 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 \n
and line \n
after line`

    // \n 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 \n" +
            "and line \n" +
            "after line"

    fmt.Print(multiline) // New lines as interpreted \n
}

Go and multiline strings

Using back ticks you can have multiline strings:

package main

import "fmt"

func main() {

    message := `This is a 
Multi-line Text String
Because it uses the raw-string back ticks 
instead of quotes.
`

    fmt.Printf("%s", message)
}

Instead of using either the double quote (“) or single quote symbols (‘), instead use back-ticks to define the start and end of the string. You can then wrap it across lines.

If you indent the string though, remember that the white space will count.

Please check the playground and do experiments with it.


You can put content with `` around it, like

var hi = `I am here,
hello,
`

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

package main

import "fmt"

func main() {
    testLine := `This is a test line 1
This is a test line 2`
    fmt.Println(testLine)
}

you can use raw literals. Example

s:=`stack
overflow`

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.
             `

참고URL : https://stackoverflow.com/questions/7933460/how-do-you-write-multiline-strings-in-go

반응형