Skip to content
/ newc Public

A code generator that generates constructor code for Golang structures.

License

Notifications You must be signed in to change notification settings

Bin-Huang/newc

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

51 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

make-constructor

Doc: English | 中文

A command-line tool to generate constructor code for a struct. It doesn't need manual installation, just add a comment line to the struct then it works.

How to use?

Just add this comment line to the struct you want to generate a constructor. It doesn't need a manual installation.

//go:generate go run github.com/Bin-Huang/[email protected]

For example:

//go:generate go run github.com/Bin-Huang/[email protected]
type UserService struct {
	baseService
	userRepository *repositories.UserRepository
	proRepository  *repositories.ProRepository
}

After go generate ./... you will get this:

// constructor_gen.go

// NewUserService Create a new UserService
func NewUserService(baseService baseService, userRepository *repositories.UserRepository, proRepository *repositories.ProRepository) *UserService {
	return &UserService{
		baseService:    baseService,
		userRepository: userRepository,
		proRepository:  proRepository,
	}
}

See more examples here

Can it be installed locally?

Go will automatically install it locally, but you can also install it manually.

go get -u github.com/Bin-Huang/make-constructor

Now you can use it like this:

//go:generate make-constructor
type UserService struct {
	baseService
	userRepository *repositories.UserRepository
	proRepository  *repositories.ProRepository
}

Wanna initialize something in the constructor?

  1. Add --init parameter
  2. Write an init method for the struct
//go:generate go run github.com/Bin-Huang/[email protected] --init
type Controller struct {
	logger *zap.Logger
	debug  bool
}

func (c *Controller) init() {
	c.logger = c.logger.With(zap.String("tag", "this-special-controller"))
	c.debug = true
}

Generated code:

// constructor_gen.go

// NewController Create a new Controller
func NewController(logger *zap.Logger, debug bool) *Controller {
	s := &Controller{
		logger: logger,
		debug:  debug,
	}
	s.init()
	return s
}

If you think the "magic comment" is too long...

Some suggestions:

  1. Add a code snippest in your editor/IDE for the tool (suggested)
  2. Install this tool manually

Features & Motivation

1. It makes your code easier to write and maintain.

Writing and updating constructor code for many structs can be laborious and error-prone, especially if you have a huge codebase. These should be handed over to automatic tools like this tool.

And it also works well with these dependency injection tools like wire. That is to say, if you use wire in your project, you may need this tool very much.

2. It takes care of the generated code.

Don't worry about the imports, variable naming, and code style in the generated code.

3. It doesn't need manual installation and another dependency.

It works anywhere there is a GO runtime and network. It doesn't break the work of other people who don't have installed this tool in collaboration.

(This tool runs locally, the network is only used to install this tool when necessary)

Sponsoring

"Buy Me A Coffee"

License

MIT

About

A code generator that generates constructor code for Golang structures.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published