Go-tagexpr is an open source Tag expression interpreter for Golang constructs by ByteDance. It is mainly used in various scenarios of the request parameter verification, and high performance, as a killer in parameter verification.

Main features:

  • Support for various common operators
  • Supports access to array, slice, and dictionary members
  • Supports access to any field in the current structure
  • Supports access to nested fields, non-exported fields, and so on
  • Built-in len, sprintf, regexp functions
  • Supports single expression and multiple expression definition mode
  • A parameter verification subpackage is provided

Let’s look at a small example of parameter verification:

package validator_test

import (
	"fmt"

	"github.com/bytedance/go-tagexpr/validator"
)

func Example(a) {
	var vd = validator.New("vd")

	type InfoRequest struct {
		Name string `vd:"($! ='Alice'||(Age)$==18) && regexp('\\w')"`
		Age  int    `vd:"$>0"`
	}
	info := &InfoRequest{Name: "Alice", Age: 18}
	fmt.Println(vd.Validate(info) == nil)
	// Output:
	// true
}
Copy the code

For more syntax details, go to Github source repository: github.com/bytedance/g…