[mirror] Make your go dev experience better github.com/olexsmir/gopher.nvim
neovim golang
1*gopher.nvim* Enhance your golang experience 2 3MIT License Copyright (c) 2025 Oleksandr Smirnov 4 5============================================================================== 6 7gopher.nvim is a minimalistic plugin for Go development in Neovim written in Lua. 8It's not an LSP tool, the main goal of this plugin is add go tooling support in Neovim. 9 10Table of Contents 11 Setup ................................................ |gopher.nvim-setup()| 12 Install dependencies ............................ |gopher.nvim-dependencies| 13 Config ................................................ |gopher.nvim-config| 14 Commands ............................................ |gopher.nvim-commands| 15 Modify struct tags ............................... |gopher.nvim-struct-tags| 16 Auto implementation of interface methods ................ |gopher.nvim-impl| 17 Generating unit tests boilerplate .................... |gopher.nvim-gotests| 18 Iferr .................................................. |gopher.nvim-iferr| 19 Generate comments ................................... |gopher.nvim-comments| 20 21------------------------------------------------------------------------------ 22 *gopher.nvim-setup()* 23 `gopher.setup`({user_config}) 24Setup function. This method simply merges default config with opts table. 25You can read more about configuration at |gopher.nvim-config| 26Calling this function is optional, if you ok with default settings. 27See |gopher.nvim.config| 28 29Usage ~ 30>lua 31 require("gopher").setup {} -- use default config or replace {} with your own 32< 33Parameters ~ 34{user_config} `(gopher.Config)` See |gopher.nvim-config| 35 36------------------------------------------------------------------------------ 37 *gopher.nvim-dependencies* 38 `gopher.install_deps` 39Gopher.nvim implements most of its features using third-party tools. 40To install these tools, you can run `:GoInstallDeps` command 41or call `require("gopher").install_deps()` if you want to use lua api. 42By default dependencies will be installed asynchronously, 43to install them synchronously pass `{sync = true}` as an argument. 44 45 46============================================================================== 47------------------------------------------------------------------------------ 48 *gopher.nvim-config* 49 `default_config` 50>lua 51 local default_config = { 52 -- log level, you might consider using DEBUG or TRACE for debugging the plugin 53 ---@type number 54 log_level = vim.log.levels.INFO, 55 56 -- timeout for running internal commands 57 ---@type number 58 timeout = 2000, 59 60 -- timeout for running installer commands(e.g :GoDepsInstall, :GoDepsInstallSync) 61 installer_timeout = 999999, 62 63 -- restart gopls server after commands like `:GoMod`, `:GoGet`, `:GoWork` 64 restart_lsp = false, 65 66 -- user specified paths to binaries 67 ---@class gopher.ConfigCommand 68 commands = { 69 go = "go", 70 gomodifytags = "gomodifytags", 71 gotests = "gotests", 72 impl = "impl", 73 iferr = "iferr", 74 }, 75 ---@class gopher.ConfigGotests 76 gotests = { 77 -- gotests doesn't have template named "default" so this plugin uses "default" to set the default template 78 template = "default", 79 -- path to a directory containing custom test code templates 80 ---@type string|nil 81 template_dir = nil, 82 -- switch table tests from using slice to map (with test name for the key) 83 named = false, 84 }, 85 ---@class gopher.ConfigGoTag 86 gotag = { 87 ---@type gopher.ConfigGoTagTransform 88 transform = "snakecase", 89 90 -- default tags to add to struct fields 91 default_tag = "json", 92 }, 93 iferr = { 94 -- choose a custom error message 95 ---@type string|nil 96 message = nil, 97 }, 98 } 99< 100Class ~ 101{gopher.Config} 102Fields ~ 103{setup} `(fun(user_config?: gopher.Config))` 104 105 106============================================================================== 107------------------------------------------------------------------------------ 108 *gopher.nvim-commands* 109 110If don't want to automatically register plugins' commands, 111you can set `vim.g.gopher_register_commands` to `false`, before loading the plugin. 112 113 114============================================================================== 115------------------------------------------------------------------------------ 116 *gopher.nvim-struct-tags* 117 118`struct_tags` is utilizing the `gomodifytags` tool to add or remove tags to struct fields. 119 120Usage ~ 121 122How to add/remove tags to struct fields: 1231. Place cursor on the struct 1242. Run `:GoTagAdd json` to add json tags to struct fields 1253. Run `:GoTagRm json` to remove json tags to struct fields 126 127To clear all tags from struct run: `:GoTagClear` 128 129NOTE: if you dont specify the tag it will use `json` as default 130 131Example: 132>go 133 // before 134 type User struct { 135 // ^ put your cursor here 136 // run `:GoTagAdd yaml` 137 ID int 138 Name string 139 } 140 141 // after 142 type User struct { 143 ID int `yaml:id` 144 Name string `yaml:name` 145 } 146< 147 148============================================================================== 149------------------------------------------------------------------------------ 150 *gopher.nvim-impl* 151 152Integration of `impl` tool to generate method stubs for interfaces. 153 154Usage ~ 1551. Automatically implement an interface for a struct: 156 - Place your cursor on the struct where you want to implement the interface. 157 - Run `:GoImpl io.Reader` 158 - This will automatically determine the receiver and implement the `io.Reader` interface. 159 1602. Specify a custom receiver: 161 - Place your cursor on the struct 162 - Run `:GoImpl w io.Writer`, where: 163 - `w` is the receiver. 164 - `io.Writer` is the interface to implement. 165 1663. Explicitly specify the receiver, struct, and interface: 167 - No need to place the cursor on the struct if all arguments are provided. 168 - Run `:GoImpl r RequestReader io.Reader`, where: 169 - `r` is the receiver. 170 - `RequestReader` is the struct. 171 - `io.Reader` is the interface to implement. 172 173Example: 174>go 175 type BytesReader struct{} 176 // ^ put your cursor here 177 // run `:GoImpl b io.Reader` 178 179 // this is what you will get 180 func (b *BytesReader) Read(p []byte) (n int, err error) { 181 panic("not implemented") // TODO: Implement 182 } 183< 184 185============================================================================== 186------------------------------------------------------------------------------ 187 *gopher.nvim-gotests* 188gotests is utilizing the `gotests` tool to generate unit tests boilerplate. 189Usage ~ 190 191- Generate unit test for specific function/method: 192 1. Place your cursor on the desired function/method. 193 2. Run `:GoTestAdd` 194 195- Generate unit tests for *all* functions/methods in current file: 196 - run `:GoTestsAll` 197 198- Generate unit tests *only* for *exported(public)* functions/methods: 199 - run `:GoTestsExp` 200 201You can also specify the template to use for generating the tests. See |gopher.nvim-config| 202More details about templates can be found at: https://github.com/cweill/gotests 203 204If you prefer named tests, you can enable them in |gopher.nvim-config|. 205 206 207============================================================================== 208------------------------------------------------------------------------------ 209 *gopher.nvim-iferr* 210 211`iferr` provides a way to way to automatically insert `if err != nil` check. 212If you want to change `-message` option of `iferr` tool, see |gopher.nvim-config| 213 214Usage ~ 215Execute `:GoIfErr` near any `err` variable to insert the check 216 217 218============================================================================== 219------------------------------------------------------------------------------ 220 *gopher.nvim-comments* 221 222This module provides a way to generate comments for Go code. 223 224Usage ~ 225Set cursor on line with function/method/struct/etc and run `:GoCmt` to generate a comment. 226 227 228 vim:tw=78:ts=8:noet:ft=help:norl: