How do you write multiline strings in Go?

前端 未结 9 928
傲寒
傲寒 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:41

    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.

提交回复
热议问题