英文:
Saving .go file in neovim with :w causes file to be messed up
问题
我对vim和Go都不太熟悉。你尝试写一个简单的go文件时,保存文件时使用:w
命令后,文件会完全乱掉。看起来并没有语法或格式错误。似乎是某个自动命令在运行,但我不知道是什么。你应该如何调试这个问题呢?以下是你的文件:
package main
import (
"net/http"
"github.com/gin-gonic/gin"
)
func main() {
router := gin.Default()
api := router.Group("/api")
{
api.GET("/hello", func (ctx *gin.Context) {
ctx.JSON(200, gin.H{"msg": "world"})
})
}
router.NoRoute(func (ctx *gin.Context) {
ctx.JSON(http.StatusNotFound, gin.H{})
})
router.Run(":8080")
}
当我在neovim中尝试使用:w
保存时,我的文件也会乱掉。以下是保存后文件的样子:
package main
import (
"net/http"
api := router.Group("/api")
"github.com/gin-gonic/gin"
func main() {
router := gin.Default()
"github.com/gin-gonic/gin"
)
func main() {
router := gin.Default()
api := router.Group("/api")
{
ctx.JSON({
gi api.GET(n/hello", fu.c (c{x *gin.Consexg) {" c}x.JSON(
.StatusNotFound, gin.H{})})
}
router.NoRoute(func (ctx *gin.Conxt) {
ctxNStaus"github.com/gin-gonic/gin"
NotFound, gin.H{})
})
router.Run(":8080")
}
我不确定发生了什么。
英文:
I am pretty new to vim and Go. I am trying to write a simple go file. When I save my file with :w
the file gets completely messed up. There doesn't seem to be any syntax or formatting errors. It seems like some autocmd is being run, but I don't know what. How would I debug this? Here is my file:
package main
import (
"net/http"
"github.com/gin-gonic/gin"
)
func main() {
router := gin.Default()
api := router.Group("/api")
{
api.GET("/hello", func (ctx *gin.Context) {
ctx.JSON(200, gin.H{"msg": "world"})
})
}
router.NoRoute(func (ctx *gin.Context) {
ctx.JSON(http.StatusNotFound, gin.H{})
})
router.Run(":8080")
}
When I try to save with :w
in neovim, my file gets messed up. Here is what the file looks like after :w
:
package main
import (
"net/http"
api := router.Group("/api")
"github.com/gin-gonic/gin"
func main() {
router := gin.Default()
"github.com/gin-gonic/gin"
)
func main() {
router := gin.Default()
api := router.Group("/api")
{
ctx.JSON({
gi api.GET(n/hello", fu.c (c{x *gin.Consexg) {" c}x.JSON(
.StatusNotFound, gin.H{})})
}
router.NoRoute(func (ctx *gin.Conxt) {
ctxNStaus"github.com/gin-gonic/gin"
NotFound, gin.H{})
})
router.Run(":8080")
}
I'm not sure what is going on.
答案1
得分: 2
这应该意味着你正在使用 vim-go
插件,默认配置下会在保存 Go 文件时自动运行 :GoFmt
命令。文件会根据 Go 的约定自动格式化。
例如,如果你保存以下文件:
package main
func main() {
fmt.Println("Hello, world!")
}
它会自动重写为正确的形式:
package main
func main() {
fmt.Println("Hello, world!")
}
此外,你可以通过在 vim 配置文件中添加以下行来配置 vim-go
使用 goimports
而不是 gofmt
:
let g:go_fmt_command = "goimports"
这不仅会格式化你的代码,还会管理你的导入语句(例如,插入任何缺失的导入)。
如果你发现自动格式化导致问题或减慢了 Vim 的速度,你可以通过在 vim 配置文件中添加以下行来禁用此功能:
let g:go_fmt_autosave = 0
在禁用保存时的自动格式化后,你仍然可以通过在 Vim 中运行 :GoFmt
命令手动格式化你的代码。
英文:
That should mean you are using the vim-go
plugin, configured by default to automatically run :GoFmt
when a Go file is saved.
The file is automatically formatted according to Go conventions.
For example, if you save this file:
package main
func main() {
fmt.Println("Hello, world!")
}
It will be automatically rewritten to the correct form:
package main
func main() {
fmt.Println("Hello, world!")
}
Additionally, you can configure vim-go
to use goimports
instead of gofmt
by adding the following line in your vim configuration file:
let g:go_fmt_command = "goimports"
This will not only format your code but also manage your imports (e.g., by inserting any missing imports).
If you find that the automatic formatting is causing issues or slowing down Vim, you can disable this feature by adding the following line to your vim configuration file:
let g:go_fmt_autosave = 0
After disabling automatic formatting on save, you can still manually format your code by running :GoFmt
within Vim.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论