embedding string const in a string const

183 views
Skip to first unread message

Brian Ketelsen

unread,
Jun 2, 2011, 9:19:42 AM6/2/11
to golang-nuts
I'm trying to embed a string const in another string const. Use case is a template that generates a Go file with a raw string literal in it.

const myTemplate = `package blah

const myEmbeddedTemplate = `the sky is crying`
`

I can't seem to find a way to escape the embedded backticks. The spec says "Within the quotes, any character is legal except back quote." and I don't see any other way to accomplish what I'm trying to do.

Any suggestions other than making the templates files and loading them? The application that is using the templates is a command, and I don't want to have to guess where the templates are.

I suppose I could make the template location a command line flag, if there are no other choices.

Input appreciated.

Brian

chris dollin

unread,
Jun 2, 2011, 9:25:59 AM6/2/11
to Brian Ketelsen, golang-nuts
On 2 June 2011 14:19, Brian Ketelsen <bket...@gmail.com> wrote:
> I'm trying to embed a string const in another string const.  Use case is a template that generates a Go file with a raw string literal in it.
>
> const myTemplate = `package blah
>
> const myEmbeddedTemplate = `the sky is crying`
> `
>
> I can't seem to find a way to escape the embedded backticks.

`sunshine` + "` backticked `" + `desserts`

Chris

--
Chris "allusive" Dollin

Brian Ketelsen

unread,
Jun 2, 2011, 10:35:19 AM6/2/11
to chris dollin, golang-nuts

That works if I strip all the newlines out of the quoted string. Fortunately it's html so it'll suffice.

Thanks!

Brian

John Asmuth

unread,
Jun 2, 2011, 10:40:48 AM6/2/11
to golan...@googlegroups.com, chris dollin
theTemplate := `some cool
stuff that might go on a few
lines
in the middle i have a 
const x = %s
that needs to be replaced
`
theRealTemplate := fmt.Sprintf(theTemplate, "`what goes inside`")

Steven

unread,
Jun 2, 2011, 12:19:29 PM6/2/11
to Brian Ketelsen, chris dollin, golang-nuts
On Thursday, June 2, 2011, Brian Ketelsen wrote:
>
> On Jun 2, 2011, at 9:25 AM, chris dollin wrote:
>
>> On 2 June 2011 14:19, Brian Ketelsen wrote:
>>>
>>>
>>> I can't seem to find a way to escape the embedded backticks.
>>
>> `sunshine` + "` backticked `" + `desserts`
>>
>> Chris
>>
>> --
>> Chris "allusive" Dollin
>
> That works if I strip all the newlines out of the quoted string.  Fortunately it's html so it'll suffice.

`sunshine` + "`" + `backticked` + "`" + `desserts`, to steal an example.

Or, you can use Sprintf to nest the strings in Go syntax, then use
Printf to output the result in Go syntax. Then, it will all be
properly escaped, so you can just paste that string into your source
file. This might be more work though.

Kyle Lemons

unread,
Jun 2, 2011, 12:42:44 PM6/2/11
to Brian Ketelsen, golang-nuts
Any SH programmers might recognize the '"'"' hack; the go-equivalent would be `+'`'+`
--
~Kyle

"Everyone knows that debugging is twice as hard as writing a program in the first place. So if you're as clever as you can be when you write it, how will you ever debug it?" 
— Brian Kernighan

Brian Ketelsen

unread,
Jun 2, 2011, 1:07:56 PM6/2/11
to golang-nuts
Thank you to everyone for the replies. I used the + concatenation.

Brian

Reply all
Reply to author
Forward
0 new messages