Skip to content
memoDwarves Foundation

Research notes/Languages

Generate tool in golang

Story about generate a file

We usually run a command line in the golang project to generate a file, such as: the mock implementation for an interface.

type UserRepo interface {
    Get(userID uint32) (*model.User, error)
    List() ([]model.User, error)
    ...
}

The old way, we make script in makefile and declare a step to generate the mock files. It's look like that

gen-mock:
    mockgen -source=./pkg/repo/repo.go -destination=./pkg/repo/mocks/repo.go
    mockgen -source=./pkg/google/service.go -destination=./pkg/google/mocks/service.go
    ...

Problem

  • We need to update the makefile for the new one. It make our makefile become huge
  • We can make the typo mistake when we type the long file path

Solution

Using the generate tool from go tool:

  1. Setup the CLI application for generate file. In this case, we are using mockgen
  2. Declare the generate tag in our source code user_repo.go
import (
    "myapp/model"
)

//go:generate mockgen -source=./user_repo.go -destination=./mocks/user_repo.go
type UserRepo interface {
    Get(userID uint32) (*model.User, error)
    List() ([]model.User, error)
    ...
}
  1. Run the go generate command
$ go generate ./...

This tool will help we generate a new mock file in our project at myapp/model/mocks/user_repo.go. We don't make our makefile become messy anymore


Reference