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