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