https://github.com/koron/iferr
Generate "if err != nil {" block
https://github.com/koron/iferr
Last synced: 4 months ago
JSON representation
Generate "if err != nil {" block
- Host: GitHub
- URL: https://github.com/koron/iferr
- Owner: koron
- License: mit
- Created: 2018-02-04T07:53:28.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2024-01-22T03:56:02.000Z (almost 2 years ago)
- Last Synced: 2025-07-10T21:27:42.028Z (6 months ago)
- Language: Go
- Homepage:
- Size: 9.77 KB
- Stars: 89
- Watchers: 3
- Forks: 15
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-list - iferr
README
# Generate "if err != nil {" block
Generate `if err != nil {` block for current function.
## Usage
Install and update by
```console
$ go install github.com/koron/iferr@latest
```
Run, it get `if err != nil {` block for the postion at 1234 bytes.
```console
$ iferr -pos 1234 < main.go
if err != nil {
return ""
}
```
```console
$ iferr -pos 1234 < main.go
if err != nil {
return ""
}
```
Customize your error message:
```console
$ iferr -pos 110 -message 'fmt.Errorf("failed to %w", err)' < main.go
if err != nil {
return 0, fmt.Errorf("failed to %w", err)
}
```
## Vim plugin
Copy `vim/ftplugin/go/iferr.vim` as `~/.vim/ftplugin/go/iferr.vim`.
It defines `:IfErr` command for go filetype. It will insert `if err != nil {`
block at next line of the cursor.
Before:
```go
package foo
import "io"
func Foo() (io.Reader, error) { // the cursor on this line.
}
```
Run `:IfErr` then you will get:
```go
package foo
import "io"
func Foo() (io.Reader, error) {
if err != nil {
return nil, err
}
} // new cursor is at here.
```