Go: Proposal: Allow backticks in raw strings (by doubling them)

Created on 7 Dec 2016  ·  10Comments  ·  Source: golang/go

This proposal is a (simple) language change: allow the following:

const x string = `a``b`

as equivalent to

const x string = "`"

That is, repeated backticks in a raw string insert a single backtick into the string.

This is currently always a syntax error, so is backwards-compatible.

FrozenDueToAge LanguageChange Proposal

Most helpful comment

Would just like to add if you're using Go templates, you can also do this if you're finding yourself needing backticks a lot (e.g. for code generation):

{{ /* Define a backtick variable */ }}
{{ $tick := "`+"`"+`" }}

type User struct {
  Name string {{ $tick }}json:"name"{{ $tick }}
}

Not the prettiest, but it's a bit more clear than the alternatives.

All 10 comments

I'm inclined to say no. This just complicates things for marginal gain. If you want to do add a backtick literal, you can do:

   const x  = `foo` + "`" + `bar`

Btw, you don't need the string part in your const declaration. It's implicit from the right-hand-side expression. See https://blog.golang.org/constants

/cc @griesemer

I'm also against this change. The beauty of raw strings is that its content is exactly what's between the `'s, nothing more, nothing less.

The main use for this is struct tags, where @bradfitz's suggestion is a syntax error.

@bradfitz: can you confirm that their is never a performance penalty from splitting a string like that?

A good example for where such a feature could be useful is if one has a string containing Go code. It is much nicer to write

`const x = ``"a
lots of stuff
with
newlines
"```

than

`const x = "\"a\nlots of stuff\nwith\nnewlines\n\"\0x60"

If you want to include ` in a struct tag, then use "" to delimit the string.

https://play.golang.org/p/j_XrQc8Eyl

It might look ugly if you also have to include "\"", but no language change
is needed.

can you confirm that their is never a performance penalty from splitting a string like that?

The compiler will merge it into one string at compile-time. There is zero additional runtime cost.

I'm going to decline this proposal as being meeting the bar for a language change. (which is quite high at this point)

@bradfitz Would like to reopen for Go 2.
Although I would find it better to escape backslash prefixing.

I would like to point out that this proposal was first and foremost rejected because of the general idea itself, not because we were waiting for Go 2.

I think this proposal wasn't sufficiently well-motivated. I would like something like this because writing reasonably long markdown strings in Go is a bit tedious. I either have everything on one line:

doc := "# `foo` class\n\nThis class does some things related to `bar`, `baz` and so on.\n\nEverything has to be on one line and I have to escape newlines which is a bit annoying.\n"

Or, essentially this monstrosity:

doc := `
# ` + "`" + `foo` + "`" + `class

This class does some things related to ` + "`" + `bar` + "`" + `, ` + "`" + `baz` + "`" + ` and so on.

This is clearly ridiculous (ok I could simplify it a bit to ` + "`bar`" + ` but it isn't much better if you have a lot of code spans in your markdown.
`

I think the original proposal (doubled backticks) is not that great. I'd say something like C++'s raw string literals is better so you can do something like:

doc := delim`
Now anything can go here including `, " and delim and the raw string continues until...
`delim

And you can choose delim to be anything (within reason). Downside to this approach is it make syntax highlighting regexes more complex.

I have another simpler alternative proposal: backticks are something you might often want in your strings, but Go source code can use all of Unicode and there are plenty of far less commonly used symbols, for example a secondary raw string syntax could be:

doc := •Raw string goes here•

I don't know how hard that is to type on US keyboards but it would be very rarely used so shouldn't matter.

Or, getting a little crazy here, how about unicode quotation marks?!

doc := “Raw string goes here”

Ok that may be a step too far!

@Timmmm If you want to make a different proposal, please open a new issue for that. This proposal is specific, and it is closed. Thanks.

Would just like to add if you're using Go templates, you can also do this if you're finding yourself needing backticks a lot (e.g. for code generation):

{{ /* Define a backtick variable */ }}
{{ $tick := "`+"`"+`" }}

type User struct {
  Name string {{ $tick }}json:"name"{{ $tick }}
}

Not the prettiest, but it's a bit more clear than the alternatives.

Was this page helpful?
0 / 5 - 0 ratings