# Option

用于快速生成 Functional Options 风格代码

# 使用

# 注解

+zz:option

# 注解对象

struct 类型对象

# 可选参数

# type

为生成的函数选项创建一个指定的类型名

示例:+zz:option:type=Option

# 示例

# 示例一

示例项目 (opens new window)

package option01

//go:generate gozz run -p "option" ./

// +zz:option
type Config struct {
	// connect host
	Host string
	// connect port
	Port string
	// database username
	Username string
	// database password
	Password string
}

执行 gozz run -p "option" ./,生成了 zzgen.option.go 文件

// Code generated by gozz:option github.com/go-zing/gozz. DO NOT EDIT.

package option01

import ()

// apply functional options for Config
func (o *Config) applyOptions(opts ...func(*Config)) {
	for _, opt := range opts {
		opt(o)
	}
}

// connect host
func WithHost(v string) func(*Config) { return func(o *Config) { o.Host = v } }

// connect port
func WithPort(v string) func(*Config) { return func(o *Config) { o.Port = v } }

// database username
func WithUsername(v string) func(*Config) { return func(o *Config) { o.Username = v } }

// database password
func WithPassword(v string) func(*Config) { return func(o *Config) { o.Password = v } }

# 示例二

示例项目 (opens new window)

对两个不同的结构体添加注解,并使用 type 选项。

package option02

//go:generate gozz run -p "option" ./

// +zz:option:type={{ .Name }}Option
type (
	Config struct {
		// connect host
		Host string
		// connect port
		Port string
		// database username
		Username string
		// database password
		Password string
	}

	Config2 struct {
		// config url
		Url string
	}
)

执行 gozz run -p "option" ./,生成了 zzgen.option.go 文件

// Code generated by gozz:option github.com/go-zing/gozz. DO NOT EDIT.

package option02

import ()

// functional options type for Config
type ConfigOption func(*Config)

// apply functional options for Config
func (o *Config) applyOptions(opts ...ConfigOption) {
	for _, opt := range opts {
		opt(o)
	}
}

// connect host
func WithHost(v string) ConfigOption { return func(o *Config) { o.Host = v } }

// connect port
func WithPort(v string) ConfigOption { return func(o *Config) { o.Port = v } }

// database username
func WithUsername(v string) ConfigOption { return func(o *Config) { o.Username = v } }

// database password
func WithPassword(v string) ConfigOption { return func(o *Config) { o.Password = v } }

// functional options type for Config2
type Config2Option func(*Config2)

// apply functional options for Config2
func (o *Config2) applyOptions(opts ...Config2Option) {
	for _, opt := range opts {
		opt(o)
	}
}

// config url
func WithUrl(v string) Config2Option { return func(o *Config2) { o.Url = v } }