[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** 43 44```lua 45require("gopher").setup { 46 commands = { 47 go = "go", 48 gomodifytags = "gomodifytags", 49 gotests = "gotests", 50 impl = "impl", 51 iferr = "iferr", 52 dlv = "dlv", 53 }, 54 gotests = { 55 -- gotests doesn't have template named "default" so this plugin uses "default" to set the default template 56 template = "default", 57 -- path to a directory containing custom test code templates 58 template_dir = nil, 59 -- switch table tests from using slice to map (with test name for the key) 60 -- works only with gotests installed from develop branch 61 named = false, 62 }, 63 gotag = { 64 transform = "snakecase", 65 }, 66} 67``` 68 69## Features 70 71<!-- markdownlint-disable --> 72 73<details> 74 <summary> 75 <b>Install plugin's go deps</b> 76 </summary> 77 78 ```vim 79 :GoInstallDeps 80 ``` 81 82 This will install the following tools: 83 84 - [gomodifytags](https://github.com/fatih/gomodifytags) 85 - [impl](https://github.com/josharian/impl) 86 - [gotests](https://github.com/cweill/gotests) 87 - [iferr](https://github.com/koron/iferr) 88 - [dlv](github.com/go-delve/delve/cmd/dlv) 89</details> 90 91<details> 92 <summary> 93 <b>Add and remove tags for structs via <a href="https://github.com/fatih/gomodifytags">gomodifytags</a></b> 94 </summary> 95 96 By default `json` tag will be added/removed, if not set: 97 98 ```vim 99 " add json tag 100 :GoTagAdd json 101 102 " remove yaml tag 103 :GoTagRm yaml 104 ``` 105 106 ```lua 107 -- or you can use lua api 108 require("gopher").tags.add "xml" 109 require("gopher").tags.rm "proto" 110 ``` 111</details> 112 113<details> 114 <summary> 115 <b>Generating tests via <a href="https://github.com/cweill/gotests">gotests</a></b> 116 </summary> 117 118 ```vim 119 " Generate one test for a specific function/method(one under cursor) 120 :GoTestAdd 121 122 " Generate all tests for all functions/methods in the current file 123 :GoTestsAll 124 125 " Generate tests for only exported functions/methods in the current file 126 :GoTestsExp 127 ``` 128 129 ```lua 130 -- or you can use lua api 131 require("gopher").test.add() 132 require("gopher").test.exported() 133 require("gopher").test.all() 134 ``` 135 136 For named tests see `:h gopher.nvim-gotests-named` 137</details> 138 139<details> 140 <summary> 141 <b>Run commands like <code>go mod/get/etc</code> inside of nvim</b> 142 </summary> 143 144 ```vim 145 :GoGet github.com/gorilla/mux 146 147 " Link can have an `http` or `https` prefix. 148 :GoGet https://github.com/lib/pq 149 150 " You can provide more than one package url 151 :GoGet github.com/jackc/pgx/v5 github.com/google/uuid/ 152 153 " go mod commands 154 :GoMod tidy 155 :GoMod init new-shiny-project 156 157 " go work commands 158 :GoWork sync 159 160 " run go generate in cwd 161 :GoGenerate 162 163 " run go generate for the current file 164 :GoGenerate % 165 ``` 166</details> 167 168<details> 169 <summary> 170 <b>Interface implementation via <a href="https://github.com/josharian/impl">impl<a></b> 171 </summary> 172 173 Syntax of the command: 174 ```vim 175 :GoImpl [receiver] [interface] 176 177 " also you can put a cursor on the struct and run 178 :GoImpl [interface] 179 ``` 180 181 Usage examples: 182 ```vim 183 :GoImpl r Read io.Reader 184 :GoImpl Write io.Writer 185 186 " or you can simply put a cursor on the struct and run 187 :GoImpl io.Reader 188 ``` 189</details> 190 191<details> 192 <summary> 193 <b>Generate boilerplate for doc comments</b> 194 </summary> 195 196 First set a cursor on **public** package/function/interface/struct and execute: 197 198 ```vim 199 :GoCmt 200 ``` 201</details> 202 203 204<details> 205 <summary> 206 <b>Generate <code>if err != nil {</code> via <a href="https://github.com/koron/iferr">iferr</a></b> 207 </summary> 208 209 Set the cursor on the line with `err` and execute 210 211 ```vim 212 :GoIfErr 213 ``` 214</details> 215 216## Contributing 217 218PRs are always welcome. See [CONTRIBUTING.md](./CONTRIBUTING.md) 219 220## Thanks 221 222- [go.nvim](https://github.com/ray-x/go.nvim) 223- [iferr](https://github.com/koron/iferr)