I would like to suggest an improvement to the built-in Go snippets. Particularly because they make gopls
(the go LSP) to cause problems, and I feel most people are using an LSP at this point, so the out of the box experience can be better here, with a simple modification.
File “Go/snippets/go-forr.sublime-snippet”
- for _, ${2:var} := range ${1:var} {
+ for _, ${2:v} := range ${1:vars} {
${0:/* code */}
This is because var
is a keyword in Go. So, when we insert this snippet and start editing at the variable being iterated over (vars
), gopls
does not function well because of syntax errors in the same line (keyword used out of place).
For example, this is what happens when you try to autocomplete digitSums
(and pressed Tab after digi
)
And the solution is as simple as replacing both var
occurrences with something else, more appropriate names than v
and vars
are up to the developer’s discretion.
Although it is a common pattern in Go to keep iterator variable names short such as one letter names. The official “A tour of go” website also conforms to this.