[mirror] Make your go dev experience better github.com/olexsmir/gopher.nvim
neovim golang
1# gopher.nvim 2 3[![Stand With Ukraine](https://raw.githubusercontent.com/vshymanskyy/StandWithUkraine/main/banner-direct-single.svg)](https://stand-with-ukraine.pp.ua) 4 5Minimalistic plugin for Go development in Neovim written in Lua. 6 7It's **NOT** an LSP tool, the main goal of this plugin is to add go tooling support in Neovim. 8 9> If you want to use new and maybe undocumented, and unstable features you might use [develop](https://github.com/olexsmir/gopher.nvim/tree/develop) branch. 10 11## Install (using [lazy.nvim](https://github.com/folke/lazy.nvim)) 12 13Requirements: 14 15- **Neovim 0.10** or later 16- Treesitter `go` parser(`:TSInstall go`) 17- [Go](https://github.com/golang/go) installed (tested on 1.23) 18 19```lua 20{ 21 "olexsmir/gopher.nvim", 22 ft = "go", 23 -- branch = "develop" 24 dependencies = { 25 "nvim-treesitter/nvim-treesitter", 26 }, 27 -- (optional) will update plugin's deps on every update 28 build = function() 29 vim.cmd.GoInstallDeps() 30 end, 31 ---@type gopher.Config 32 opts = {}, 33} 34``` 35 36## Configuration 37 38> [!IMPORTANT] 39> 40> If you need more info look `:h gopher.nvim` 41 42**Take a look at default options (might be a bit outdated, look `:h gopher.nvim-config`)** 43 44```lua 45require("gopher").setup { 46 -- log level, you might consider using DEBUG or TRACE for debugging the plugin 47 log_level = vim.log.levels.INFO, 48 49 -- timeout for running internal commands 50 timeout = 2000, 51 52 commands = { 53 go = "go", 54 gomodifytags = "gomodifytags", 55 gotests = "gotests", 56 impl = "impl", 57 iferr = "iferr", 58 }, 59 gotests = { 60 -- gotests doesn't have template named "default" so this plugin uses "default" to set the default template 61 template = "default", 62 -- path to a directory containing custom test code templates 63 template_dir = nil, 64 -- switch table tests from using slice to map (with test name for the key) 65 named = false, 66 }, 67 gotag = { 68 transform = "snakecase", 69 -- default tags to add to struct fields 70 default_tag = "json", 71 }, 72 iferr = { 73 -- choose a custom error message 74 message = nil, 75 }, 76} 77``` 78 79## Features 80 81<!-- markdownlint-disable --> 82 83<details> 84 <summary> 85 <b>Install plugin's go deps</b> 86 </summary> 87 88 ```vim 89 :GoInstallDeps 90 ``` 91 92 This will install the following tools: 93 94 - [gomodifytags](https://github.com/fatih/gomodifytags) 95 - [impl](https://github.com/josharian/impl) 96 - [gotests](https://github.com/cweill/gotests) 97 - [iferr](https://github.com/koron/iferr) 98</details> 99 100<details> 101 <summary> 102 <b>Add and remove tags for structs via <a href="https://github.com/fatih/gomodifytags">gomodifytags</a></b> 103 </summary> 104 105 By default `json` tag will be added/removed, if not set: 106 107 ```vim 108 " add json tag 109 :GoTagAdd json 110 111 " remove yaml tag 112 :GoTagRm yaml 113 ``` 114 115 ```lua 116 -- or you can use lua api 117 require("gopher").tags.add "xml" 118 require("gopher").tags.rm "proto" 119 ``` 120</details> 121 122<details> 123 <summary> 124 <b>Generating tests via <a href="https://github.com/cweill/gotests">gotests</a></b> 125 </summary> 126 127 ```vim 128 " Generate one test for a specific function/method(one under cursor) 129 :GoTestAdd 130 131 " Generate all tests for all functions/methods in the current file 132 :GoTestsAll 133 134 " Generate tests for only exported functions/methods in the current file 135 :GoTestsExp 136 ``` 137 138 ```lua 139 -- or you can use lua api 140 require("gopher").test.add() 141 require("gopher").test.exported() 142 require("gopher").test.all() 143 ``` 144 145 For named tests see `:h gopher.nvim-gotests-named` 146</details> 147 148<details> 149 <summary> 150 <b>Run commands like <code>go mod/get/etc</code> inside of nvim</b> 151 </summary> 152 153 ```vim 154 :GoGet github.com/gorilla/mux 155 156 " Link can have an `http` or `https` prefix. 157 :GoGet https://github.com/lib/pq 158 159 " You can provide more than one package url 160 :GoGet github.com/jackc/pgx/v5 github.com/google/uuid/ 161 162 " go mod commands 163 :GoMod tidy 164 :GoMod init new-shiny-project 165 166 " go work commands 167 :GoWork sync 168 169 " run go generate in cwd 170 :GoGenerate 171 172 " run go generate for the current file 173 :GoGenerate % 174 ``` 175</details> 176 177<details> 178 <summary> 179 <b>Interface implementation via <a href="https://github.com/josharian/impl">impl<a></b> 180 </summary> 181 182 Syntax of the command: 183 ```vim 184 :GoImpl [receiver] [interface] 185 186 " also you can put a cursor on the struct and run 187 :GoImpl [interface] 188 ``` 189 190 Usage examples: 191 ```vim 192 :GoImpl r Read io.Reader 193 :GoImpl Write io.Writer 194 195 " or you can simply put a cursor on the struct and run 196 :GoImpl io.Reader 197 ``` 198</details> 199 200<details> 201 <summary> 202 <b>Generate boilerplate for doc comments</b> 203 </summary> 204 205 First set a cursor on **public** package/function/interface/struct and execute: 206 207 ```vim 208 :GoCmt 209 ``` 210</details> 211 212 213<details> 214 <summary> 215 <b>Generate <code>if err != nil {</code> via <a href="https://github.com/koron/iferr">iferr</a></b> 216 </summary> 217 218 Set the cursor on the line with `err` and execute 219 220 ```vim 221 :GoIfErr 222 ``` 223</details> 224 225## Contributing 226 227PRs are always welcome. See [CONTRIBUTING.md](./CONTRIBUTING.md) 228 229## Thanks 230 231- [go.nvim](https://github.com/ray-x/go.nvim) 232- [iferr](https://github.com/koron/iferr)