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