A lightweight Golang neovim plugin that focuses on simplicity and efficiency
Neovim >= 0.9.0
{ 'shoukoo/g0.nvim',
config = function ()
require("g0").setup()
end
}You can also modify the defaults config
{ 'shoukoo/g0.nvim',
config = function ()
require("g0").setup({
gomodifytags = {
tags = "xml,json"
}
})
end
}g0 comes with the following defaults
{
gotest = {
-- run go test in verbose mode
verbose = false
},
gomodifytags = {
-- https://github.com/fatih/gomodifytags#transformations
-- Transform adds a transform rule when adding tags.
-- Current options: [snakecase, camelcase, lispcase, pascalcase, titlecase, keep]
transform = "snakecase",
-- Add/Remove tags for the comma separated list of keys. i.e.: json,xml
tags = "json",
-- Add/Remove the options per given key. i.e: json=omitempty,hcl=squash
options = ""
},
-- timeout in seconds, mainly used by goimports
timeout = 1000
debug = false,
}Copy below to run goimports on save
local format_sync_grp = vim.api.nvim_create_augroup("G0Import", {})
vim.api.nvim_create_autocmd("BufWritePre", {
pattern = "*.go",
callback = function()
local success, result = pcall(function()
require('g0.format').goimports()
end)
if not success then
vim.notify(result, vim.log.levels.ERROR)
end
end,
group = format_sync_grp,
})These are the available packages that can be installed using the command. To add a new package, you can add it in the lua/g0/install.lua, or manually install it
| Pkg | Repository URL |
|---|---|
| goimports | golang.org/x/tools/cmd/goimports |
| gomodifytags | github.com/fatih/gomodifytags |
| gopls | golang.org/x/tools/gopls |
| delve | github.com/go-delve/delve/cmd/dlv |
To install the goimports pkg
:G0Install goimportsTo install the gopls pkg
:G0Install gopls Install all the available packages
Update all the available packages
Running :G0TestCurrent executes the cd {file dir} && go test -run <func name> command in the directory of the current file. You can also provide additional valid flags as needed, which are documented in go help test.
Run the go test with the verbose flag
:G0TestCurrent -vRun the go test with the integration tag
:G0TestCurrent --tag=integrationRunning :G0TestCurrentDir executes the cd {file dir} && go test ./... command in the directory of the current file. You can also provide additional valid flags as needed, which are documented in go help test.
Run the go test with the verbose flag
:G0TestCurrentDir -vRun the go test with the integration tag
:G0TestCurrentDir --tag=integrationDisplay the past Go test results from both the G0TestCurrentDir and G0TestCurrent commands.
Running plain :G0AddTags executes the gomodifyfiles -file={file dir} -add-tags=json command to the current file. You can also provide additional valid flags as needed, which are documented in the gomodifytags repo. Please note that it utilises default values from gomodifytags; for instance, it defaults to using snake_case for the tags
To add tags to the entire struct, position your cursor at the beginning of the struct and run the cmd below. To add a tag to a specific field, move your cursor to that field run the same cmd.
:G0AddTagsHighlight the fields in visual mode, and then execute the following command to add tags to them.
:'<,'>G0AddTagsTo add both xml and json tags, run the cmd below
:G0AddTags -add-tags=xml,jsonTo use camelCase for the tags, run the cmd below
:G0AddTags -transform=camelcasePlain G0RemoveTags executes the gomodifytags -file={file} -remove-tags=json command to the current file. You can also override the flags as needed, you can find additional flags from the gomodifytags repo
To remove json tags to the entire struct, position your cursor at the beginning of the struct and run the cmd below.
:G0RemoveTagsHighlight the fields in visual mode, and then execute the following command to remove json tags to them.
:'<,'>G0RemoveTagsTo remove both xml and json tags, run the cmd below
:G0RemoveTags -remove-tags=xml,jsonPlain G0ClearTags executes the gomodifytags -file={file} -clear-tags command to the current file. You can also override the flags as needed, you can find additional flags from the gomodifytags repo
To clear tags to the entire struct, position your cursor at the beginning of the struct and run the cmd below.
:G0ClearTagsHighlight the fields in visual mode, and then execute the following command to remove json tags to them.
:'<,'>G0ClearTagsTo remove both xml and json tags, run the cmd below
:G0ClearTags