Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

proposal: Go 2: spec: turn double backquote into single backquote in raw string literals #23228

Closed
rhysd opened this issue Dec 23, 2017 · 6 comments
Labels
FrozenDueToAge LanguageChange Proposal v2 A language change or incompatible library change
Milestone

Comments

@rhysd
Copy link
Contributor

rhysd commented Dec 23, 2017

Please answer these questions before submitting your issue. Thanks!

What version of Go are you using (go version)?

go version go1.9.2 darwin/amd64

What did you expect to see?

I wanted to embed some markdown documents easily as string literals in Go source code such as:

Please use `foo` for calling `do_something` in script

```
do_something(foo)
```

What did you see instead?

Current raw string literal cannot contain back quotes. So I needed to split the string literal into some parts; which does not contain back quotes and which contains back quotes as following:

markdownDoc := `
Please use ` + "`foo` for calling `do_something`" + ` in script

` + "```" + `
do_something(foo)
` + "```" + `

...
`

It was so hard to write/read many back quotes with switching cotexts ("" and ``). But I needed to use raw string here because I also wanted to contain newlines.

Thus, embedding backs quotes in multi-line string literals is hard in Go source code. But it's not so rare case to use back quotes in multi-line string in real world (i.e. markdown documents, shell scripts, help documents, ...).

My proposal

To solve above problem, I want to add a non-breaking change to spec of raw string literal.

To enable raw string literals to contain backquotes, I want to propose that doubled back quotes '``' means one back quote as following:

fmt.Println(`foo `` bar`) // Equivalent to "foo ` bar"
fmt.Println(````) // Equivalent to "`"

By this change, my previous problem would be solved as following:

markdownDoc := `
Please use ``foo`` for calling ``do_something`` in script

``````
do_something(foo)
``````

...
`

This change should not cause any breaking change because any raw string literals which contain doubled back quotes currently cause a compilation error.

I've already created a patch to implement this proposal to check my proposal is useful. So I'll submit it to Gerrit soon. As long as from my point of view, it is useful 👍

@gopherbot gopherbot added this to the Proposal milestone Dec 23, 2017
@gopherbot
Copy link

Change https://golang.org/cl/85435 mentions this issue: go/scanner: allow backtick in raw string literal

rhysd added a commit to rhysd/go that referenced this issue Dec 23, 2017
This commit enables to embed ` in raw string literal. Doubled backtick
`` now means one ` in raw string literal as following.

// Outputs 'foo `bar'
fmt.Println(`foo `` bar`)

// Outputs '`'
fmt.Println(````)

// Outputs '``'
fmt.Println(``````)

GitHub issue for proposal of this feature:
  golang#23228

Fixes golang#23228

Change-Id: I5708cb3538fb49ca8669ad56e22b15a36461925a
@ianlancetaylor ianlancetaylor changed the title proposal: Enable to contain back quotes in raw string literals proposal: Go 2: spec: turn double backquote into single backquote in raw string literals Dec 23, 2017
@ianlancetaylor ianlancetaylor added v2 A language change or incompatible library change LanguageChange labels Dec 23, 2017
@rhysd
Copy link
Contributor Author

rhysd commented Dec 24, 2017

@ianlancetaylor Could you tell me why this issue got Go2 label? If my understanding is correct, this change does not any breaking change.

@cznic
Copy link
Contributor

cznic commented Dec 24, 2017

It breaks all existing 3rd party lexers/parsers and any tool using them. It also breaks every tool that assumes a back tick cannot appear in the value of a raw string literal, etc.

@dominikh
Copy link
Member

It's also a language change. Pretty much all language changes are reserved for Go 2, simply because Go 1 is considered "done". This even applies to changes that are backwards compatible.

@extemporalgenome
Copy link
Contributor

This breaks the very nice property that raw strings are completely uninterpreted, and would have to set a new precedent in Go in which doubling a special character is the way to escape it.

Some other languages have fulfilled your need with, e.g. triple quoted strings, which may be more consistent with the rest of Go than the proposed form of escaping.

@ianlancetaylor
Copy link
Contributor

Raw strings right now are very simple. This would make them more complicated. As you note, you can always use +.

@golang golang locked and limited conversation to collaborators Apr 17, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge LanguageChange Proposal v2 A language change or incompatible library change
Projects
None yet
Development

No branches or pull requests

6 participants