Giter Site home page Giter Site logo

newc's Introduction

newc

new-* constructor generator

Doc: English | δΈ­ζ–‡


A code generator that generates constructor code for Golang structures.

Installation

go install github.com/Bin-Huang/newc@latest

Usage

Add a go:generate command line to the struct which you want to generate a constructor.

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

After executing go generate ./... the constructor code generated:

// 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

Usage without manual installation

Recommended for team collaboration

Without manual installation, just add this comment line to the struct. Go will automatically install this tool if missing.

//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
}

This is very useful, especially in teamwork. It can run without manual installation. It doesn't break the work of other people who don't have installed this tool in collaboration.

How to return value instead reference?

Add --value parameter

//go:generate newc --value
type Config struct {
	debug  bool
}

Generated code:

// constructor_gen.go

// NewConfig Create a new Config
func NewConfig(debug bool) Config {
	return Config{
		debug:  debug,
	}
}

How to call an initializer in constructor?

  1. Add --init parameter
  2. Write an init method for the struct
//go:generate newc --init
type Controller struct {
	logger *zap.Logger
	debug  bool
}

func (c *Controller) init() {
	c.logger = c.logger.With(zap.String("tag", "controller-debugger"))
	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
}

How to ignore some fields when generating constructor code?

Add a tag newc:"-" to fields that need to be ignored

type Forbidden struct {
	Msg    string
	Status int    `newc:"-"`
}

Generated code:

// NewForbidden Create a new Forbidden
func NewForbidden(msg string) *Forbidden {
	return &Forbidden{
		Msg: msg,
	}
}

If you think the go:generate comment is too long...

Some suggestions:

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

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. 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 is more suitable for teamwork.

It doesn't break the work of other people who don't have installed this tool in collaboration. Go will automatically install this tool if missing.

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

Sponsoring

"Buy Me A Coffee"

License

MIT

newc's People

Contributors

bin-huang avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

newc's Issues

let return value's type to be an interface

Like the struct myDoer below:

type Doer interface {
    Do() (int, error)
}

type myDoer struct {
    v int
}

func (r *myDoer) Do() (int, error) {
    // ...
    return 0, nil
}

And myReader's constructor sig shall be like:

func NewMyDoer(v int) Doer

instead of

func NewMyDoer(v int) *myDoer

It can be an option, and user can choose its constructor to return an pointer or an designated interface.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.