Sublime Forum

Improvements in Go snippet `go-forr`

#1

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)
image

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.

1 Like

#2

Feel free to open a PR at https://github.com/sublimehq/Packages/blob/master/Go/Snippets/go-forr.sublime-snippet

0 Likes

#3

Thanks! I didn’t know this part was open source, thought it was built-ins.

I’ve created PR #4264.

0 Likes