golang heredocs?



Heredocs for the Go programming language?
package main
import "fmt"

func main() {
    myline := `
    the quick
    brown fox
    jumps over
    the lazy dog
    `
    fmt.Println(myline)
}

To Println multiple lines of strings in golang, use back ticks.

I am not sure if I got this right. I just read this somewhere on stackoverflow(i'll add the source link when I find it again) and decided to test it. The code above prints several lines, but with plenty of white space.
j@ime:~$ go run a.go

    the quick
    brown fox
    jumps over
    the lazy dog


I will look into how to make this work better... just multiple lines of string without the extra white space. I'll update when I figure it out.

1 comment:

  1. You get all the white space in your output you put between the backticks. I think you want this:

    func main() {
    myline := `the quick
    brown fox
    jumps over
    the lazy dog
    `
    fmt.Println(myline)
    }

    I do hope this comes correctly through. Neither pre nor code tag is allowed so I cannot control the format.

    ReplyDelete