The package ctx
was adding to Golang after people had started using requests.
In order to use ctxerr you need context passed with every function that returns and error.
This is a find and replace that adds ctx context.Context
to all functions that return an error.
- Open VSCode or Curser and do an editor serach
cmd + shift + f
- Set it to regex find by clicking the
.*
button - Paste find below as the search
- In
files to include
add*.go
- In
files to exclude
addmock_*.go, *_test.go
- Click the
>
to allow replace - In the replace field paste below
- The
$1
and$4
are the matched groups in()
- The
- Make sure you didn't change interface functions like
MarshalJSON
or handler funcs
- Empty parameter
- Find -
(func (\(.*\) )*[A-za-z]+)(\()(?!ctx)(\).*error.*\{)
- Replace -
$1(ctx context.Context$4
- Find -
- Has parameters (needs a comma)
- Find -
(func (\(.*\) )*[A-za-z]+)(\()(?!ctx)(.*[a-z]+\).*error.*\{)
- Replace -
$1(ctx context.Context, $4
- Find -
In regex101 I was matching against this list
func Foo() {
func Foo() error {
func (h *hello) Foo() {
func (h *hello) Foo() error {
func (h *hello) Foo(s string) error {
func (h *hello) Foo() (int, error) {
func Foo() string {
func Foo() (string, int) {
func Foo(s string) (string, error) {
func Foo(ctx context.Context) {
func Foo(ctx context.Context) error {
func (h *hello) Foo(ctx context.Context) {
func (h *hello) Foo(ctx context.Context) error {
func (h *hello) Foo(ctx context.Context, s string) error {
func (h *hello) Foo(ctx context.Context) (int, error) {
func Foo(ctx context.Context) string {
func Foo(ctx context.Context) (string, int) {
func Foo(ctx context.Context, s string) (string, error) {