Join GitHub today
GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.Sign up
GitHub is where the world builds software
Millions of developers and companies build, ship, and maintain their software on GitHub — the largest and most advanced development platform in the world.
proposal: Go 2: prefixed labels for backtick literals #40393
For this, backtick literals are helpful as you can format your code as desired:
var script =`console.log('Hello world');` var page = ` <html> <head> <title>Title</title> </head> <body> <p>Hello, world!</p> </body> </html> `
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:
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:
10000 == 10_000 // true "contents" == foo`contents` // true
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.