是否可以在vim中为go代码和注释设置不同的textwidths?

For example, textwidth=100 for go code and textwidth=80 for go comment.

With my OnSyntaxChange plugin, you can trigger settings changes based on the syntax element you're currently in.

call OnSyntaxChange#Install('GoComment', '^Comment$', 1, 'a')
autocmd User SyntaxGoCommentEnterA setlocal textwidth=80
autocmd User SyntaxGoCommentLeaveA setlocal textwidth=100

Put this into ~/.vim/after/ftplugin/go.vim. (This requires that you have :filetype plugin on. Alternatively, you could define an :autocmd FileType go ... (for each line) directly in your ~/.vimrc, but this tends to become unwieldy once you have many customizations.

Alternative

A smaller, non-plugin variant would be a custom gq mapping that changes 'textwidth' temporarily, performs the reformatting, and then restores the original value. But that only works for manually triggered reformatting of paragraphs.