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