0

I have a go application reading content from a Markdown file and adding it to a go template file.

var tmpl = template.Must(template.New("").Parse(`//Code generated by go generate
//Please don't change

package cmd

var fooTemplate = ` + "`" + `{{ .FileName }}` + "`"))

and the markdown file has content

## Title
Foo bar

## Description
This is a dummy description using `go` language

The problem is that the markdown syntax has backticks and so the generated go file is broken in syntax. This syntax works here in SO because of the use of triple backticks(```) but doesn't while generating the go file.

Is it possible to create a go file with backticks inside the multiline string by escaping them?

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Aditya Singh
  • 15,810
  • 15
  • 45
  • 67
  • 2
    No. Backtick literals have no escaping support whatsoever. Interrupting the literal like you've done in your question is one way to do it, and fmt.Sprintf is another option. – Peter Sep 27 '19 at 15:31
  • Split string on lines, escape each line with https://godoc.org/strconv#Quote and join with `+`. – Charlie Tumahai Sep 27 '19 at 15:35

0 Answers0