-
Notifications
You must be signed in to change notification settings - Fork 18k
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: prefixed labels for backtick literals #40393
Comments
Another approach would be a comment. This wouldn't require changing the language. //go:syntax html
var page = `
<html>
<head>
<title>Title</title>
</head>
<body>
<p>Hello, world!</p>
</body>
</html>
` |
I think the benefit of formally adding to the language is that how to label a string becomes explicit. There wouldn't be special rules around how to format or place comments (before? after? either?) in order to get the benefit. With that clarity, I believe better tooling and features could be derived. |
I agree with @icholy that additional text with no semantic meaning is better handle by a comment. The go/parser package already provides a well documented mechanism for commenting variables: the same as doc comments. And in 1.15 Go documentation packages don't display comments that start with |
I do think #37974 makes using comments much more palatable.
Playing with it, I came across an example where a comment approach is ambiguous: _, err := c.exec(ctx, `
SELECT *
FROM orders
WHERE state = $1 AND id = $2;
`, /*go:syntax sql*/ "pending", id) When trying to put the comment adjacent to the backtick literal, it jumps behind or in front of nearby commas after Obviously, this string could be refactored out of being inline and that could just be the rule, but with a prefix labeling approach, it would just work and be clear in all cases I can think of. Related, most IDEs won't have access to an AST—so support for a comment-based approach might have to come in the form of syntax definitions based on regular expressions. Obviously not impossible, but probably pretty rough to get right. |
A simple rule would be that
That would be easy for a regex. |
backtick literals seem to have been proposed in #32590 already. |
Closing in favor of #32590. |
It's common in Go source code to write code in other languages like JSON, SQL, HTML, or JavaScript.
For this, backtick literals are helpful as you can format your code as desired:
This proposal is a simple change to allow an optional prefixed label for backtick literals:
Allowing prefixed labels enables IDEs and language parsers to act on the string contents:
gofmt
, a program could format the string contents automatically by inferring the type from the label.Like the 1.13 change which allows underscores between digits, prefixed labels are meant as a syntactic hint which has no effect on the string value itself, that is:
The text was updated successfully, but these errors were encountered: