[mirror] Make your go dev experience better github.com/olexsmir/gopher.nvim
neovim golang
at main 11 kB view raw
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 json2go .............................................. |gopher.nvim-json2go| 17 Auto implementation of interface methods ................ |gopher.nvim-impl| 18 Generating unit tests boilerplate .................... |gopher.nvim-gotests| 19 Iferr .................................................. |gopher.nvim-iferr| 20 Generate comments ................................... |gopher.nvim-comments| 21 22------------------------------------------------------------------------------ 23 *gopher.nvim-setup()* 24 `gopher.setup`({user_config}) 25Setup function. This method simply merges default config with opts table. 26You can read more about configuration at |gopher.nvim-config| 27Calling this function is optional, if you ok with default settings. 28See |gopher.nvim.config| 29 30Usage ~ 31>lua 32 require("gopher").setup {} -- use default config or replace {} with your own 33< 34Parameters ~ 35{user_config} `(gopher.Config)` See |gopher.nvim-config| 36 37------------------------------------------------------------------------------ 38 *gopher.nvim-dependencies* 39 `gopher.install_deps` 40 41Gopher.nvim implements most of its features using third-party tools. To 42install plugin's dependencies, you can run: 43`:GoInstallDeps` or `:GoInstallDepsSync` 44or use `require("gopher").install_deps()` if you prefer lua api. 45 46 47============================================================================== 48------------------------------------------------------------------------------ 49 *gopher.nvim-config* 50 `default_config` 51>lua 52 local default_config = { 53 -- log level, you might consider using DEBUG or TRACE for debugging the plugin 54 ---@type number 55 log_level = vim.log.levels.INFO, 56 57 -- timeout for running internal commands 58 ---@type number 59 timeout = 2000, 60 61 -- timeout for running installer commands(e.g :GoDepsInstall, :GoDepsInstallSync) 62 ---@type number 63 installer_timeout = 999999, 64 65 -- user specified paths to binaries 66 ---@class gopher.ConfigCommand 67 commands = { 68 go = "go", 69 gomodifytags = "gomodifytags", 70 gotests = "gotests", 71 impl = "impl", 72 iferr = "iferr", 73 json2go = "json2go", 74 }, 75 ---@class gopher.ConfigGotests 76 gotests = { 77 -- a default template that gotess will use. 78 -- gotets doesn't have template named `default`, we use it to represent absence of the provided template. 79 template = "default", 80 81 -- path to a directory containing custom test code templates 82 ---@type string|nil 83 template_dir = nil, 84 85 -- use named tests(map with test name as key) in table tests(slice of structs by default) 86 named = false, 87 }, 88 ---@class gopher.ConfigGoTag 89 gotag = { 90 ---@type gopher.ConfigGoTagTransform 91 transform = "snakecase", 92 93 -- default tags to add to struct fields 94 default_tag = "json", 95 96 -- default tag option added struct fields, set to nil to disable 97 -- e.g: `option = "json=omitempty,xml=omitempty` 98 ---@type string|nil 99 option = nil, 100 }, 101 ---@class gopher.ConfigIfErr 102 iferr = { 103 -- choose a custom error message, nil to use default 104 -- e.g: `message = 'fmt.Errorf("failed to %w", err)'` 105 ---@type string|nil 106 message = nil, 107 }, 108 ---@class gopher.ConfigJson2Go 109 json2go = { 110 -- command used to open interactive input. 111 -- e.g: `split`, `botright split`, `tabnew` 112 interactive_cmd = "vsplit", 113 114 -- name of autogenerated struct, if nil none, will the default one of json2go. 115 -- e.g: "MySuperCoolName" 116 ---@type string|nil 117 type_name = nil, 118 }, 119 } 120< 121Class ~ 122{gopher.Config} 123Fields ~ 124{setup} `(fun(user_config?: gopher.Config))` 125 126 127============================================================================== 128------------------------------------------------------------------------------ 129 *gopher.nvim-commands* 130 131If don't want to automatically register plugins' commands, 132you can set `vim.g.gopher_register_commands` to `false`, before loading the plugin. 133 134 135============================================================================== 136------------------------------------------------------------------------------ 137 *gopher.nvim-struct-tags* 138 139`struct_tags` is utilizing the `gomodifytags` tool to add or remove tags to 140struct fields. 141 142Usage ~ 143 144How to add/remove/clear tags to struct fields: 1451. Place cursor on the struct 1462. Run `:GoTagAdd json` to add json tags to struct fields 1473. Run `:GoTagRm json` to remove json tags to struct fields 1484. Run `:GoTagClear` to clear all tags from struct fields 149 150If you want to add/remove tag with options, you can use `json=omitempty` 151(where json is tag, and omitempty is its option). 152Example: `:GoTagAdd xml json=omitempty` 153 154 155NOTE: if you dont specify the tag it will use `json` as default 156 157Example: 158>go 159 // before 160 type User struct { 161 // ^ put your cursor here 162 // run `:GoTagAdd yaml` 163 ID int 164 Name string 165 } 166 167 // after 168 type User struct { 169 ID int `yaml:id` 170 Name string `yaml:name` 171 } 172< 173 174============================================================================== 175------------------------------------------------------------------------------ 176 *gopher.nvim-json2go* 177 178Convert json to go type annotations. 179 180Usage ~ 181 182`:GoJson` opens a temporary buffer where you can paste or write JSON. 183Saving the buffer (`:w` or `:wq`) automatically closes it and inserts the 184generated Go struct into the original buffer at the cursor position. 185 186Alternatively, you can pass JSON directly as an argument: 187>vim 188 :GoJson {"name": "Alice", "age": 30} 189< 190------------------------------------------------------------------------------ 191 *json2go.transform()* 192 `json2go.transform`({json_str}) 193 194Parameters ~ 195{json_str} `(string)` Json string that is going to be converted to go type. 196Return ~ 197`(string)` `(optional)` Go type, or nil if failed. 198 199------------------------------------------------------------------------------ 200 *json2go.json2go()* 201 `json2go.json2go`({json_str}) 202Converts json string to go type, and puts result under the cursor. If 203[json_str] is nil, will open an interactive prompt (with cmd set in 204config). 205 206Parameters ~ 207{json_str} `(optional)` `(string)` 208 209 210============================================================================== 211------------------------------------------------------------------------------ 212 *gopher.nvim-impl* 213 214Integration of `impl` tool to generate method stubs for interfaces. 215 216Usage ~ 217 2181. Automatically implement an interface for a struct: 219 - Place your cursor on the struct where you want to implement the interface. 220 - Run `:GoImpl io.Reader` 221 - This will automatically determine the receiver and implement the `io.Reader` interface. 222 2232. Specify a custom receiver: 224 - Place your cursor on the struct 225 - Run `:GoImpl w io.Writer`, where: 226 - `w` is the receiver. 227 - `io.Writer` is the interface to implement. 228 2293. Explicitly specify the receiver, struct, and interface: 230 - No need to place the cursor on the struct if all arguments are provided. 231 - Run `:GoImpl r RequestReader io.Reader`, where: 232 - `r` is the receiver. 233 - `RequestReader` is the struct. 234 - `io.Reader` is the interface to implement. 235 236Example: 237>go 238 type BytesReader struct{} 239 // ^ put your cursor here 240 // run `:GoImpl b io.Reader` 241 242 // this is what you will get 243 func (b *BytesReader) Read(p []byte) (n int, err error) { 244 panic("not implemented") // TODO: Implement 245 } 246< 247 248============================================================================== 249------------------------------------------------------------------------------ 250 *gopher.nvim-gotests* 251gotests is utilizing the `gotests` tool to generate unit tests boilerplate. 252Usage ~ 253 254- Generate unit test for specific function/method: 255 1. Place your cursor on the desired function/method. 256 2. Run `:GoTestAdd` 257 258- Generate unit tests for *all* functions/methods in current file: 259 - run `:GoTestsAll` 260 261- Generate unit tests *only* for *exported(public)* functions/methods: 262 - run `:GoTestsExp` 263 264You can also specify the template to use for generating the tests. 265See |gopher.nvim-config|. 266More details about templates: https://github.com/cweill/gotests 267 268If you prefer named tests, you can enable them in |gopher.nvim-config|. 269 270 271============================================================================== 272------------------------------------------------------------------------------ 273 *gopher.nvim-iferr* 274 275`iferr` provides a way to way to automatically insert `if err != nil` check. 276If you want to change `-message` option of `iferr` tool, see |gopher.nvim-config| 277 278Usage ~ 279Execute `:GoIfErr` near any `err` variable to insert the check 280 281 282============================================================================== 283------------------------------------------------------------------------------ 284 *gopher.nvim-comments* 285 286This module provides a way to generate comments for Go code. 287 288Usage ~ 289 290Set cursor on line with function/method/struct/etc and 291run `:GoCmt` to generate a comment. 292 293 294 vim:tw=78:ts=8:noet:ft=help:norl: