Friday, 10 May 2019

Go Language: Multi line strings


You can write multi line strings by placing the string in backticks operator.

Example
    msg := `Hello
    World,
    How
    Are
    You`

    fmt.Println(msg)

App.go
package main

import "fmt"

func main() {
    msg := `Hello
    World,
    How
    Are
    You`

    fmt.Println(msg)
}

Output
Hello
        World,
        How
        Are
        You

Backticks will not interpret escape characters

App.go
package main

import "fmt"

func main() {
    msg := `Hello
    \n World,
    \t How
    \a Are
    You`

    fmt.Println(msg)
}

aOutput
Hello
        \n World,
        \t How
        \a Are
        You

If you want to preserve escape sequences, you can use ‘+’ operator.

    msg := "Hello " +
        "\n World" +
        "\t How" +
        "\a Are" +
        "You"


App.go
package main

import "fmt"

func main() {
    msg := "Hello " +
        "\n World" +
        "\t How" +
        "\a Are" +
        "You"

    fmt.Println(msg)
}

Output
Hello
  World   How AreYou

Previous                                                 Next                                                 Home

No comments:

Post a Comment