Github.com/zlyuancn/zs…


String tool

The sample

s := zstr.String("1")
s.Val()     / / for val
s.GetBool() / / get a bool
s.GetInt()  / / get the int
var a float64
_ = s.Scan(&a) // Scan to a
Copy the code

Quick function

zstr.GetString("1")
zstr.GetBool("1")
zstr.GetInt("1")
var a float64
zstr.Scan("1", &a)
Copy the code

Other methods

The GetXXX method doesn’t just support string; it can pass in any type

GetInt  GetInt8  GetInt16  GetInt32  GetInt64
GetUint  GetUint8  GetUint16  GetUint32  GetUint64
GetFloat32  GetFloat64
GetString
Copy the code

Boolean support

1, t, t, true, true, y, y, yes, yes, yes, on, on, OK, OK, ok, ok FALSE, False, n, N, no, NO, No, off, OFF, OffCopy the code

Template rendering

The sample

Render("s@a e".map[string]string{"a": "va"})
Render("s{@a}e".map[string]string{"a": "va"})
Render("s{@a}e"."va")
Render("s@a @a e"."va0"."va1")
Copy the code

The variable name

Template variables start with @, and variable names support upper and lower case letters. Digital; The underline. The decimal point. Template variables can be wrapped in curly braces' {} 'to precisely define the beginning and end of template variables. Note that there are no Spaces in curly braces. Example: @a@[email protected] {@a.... c} {@9} @0... ACopy the code

Template Rendering instructions

func Render(format string, values ...interface{}) string
// format indicates the text to be rendered
// values indicates the variable value
Copy the code

Variable name matching

The variable name is subscript, and the subscript starts from 0. For example, A [0] represents the first occurrence of A, and a[5] represents the sixth occurrence of A. Equal to the variable name, for example: a; a_b; A.c; a.... c; 9. 0... A The asterisk * can match any variable name, but it must be used with A subscript that starts at 0. For example, *[0] indicates one variable, and *[5] indicates the sixth variable.Copy the code

Variable names match priorities

  1. Variable names with subscripts
  2. The variable name
  3. Asterisks with subscripts

value

Reference MakeMapOfValues

Any type of map is supported, for example, map[string]interface{}; The map (int) int, map [string] int, etc. Preach and support order value, such as Render (" to Render the text ", value of 0, 1, 2) value, it will be converted to the following description map [string] interface {} {" * [0] ": the value of 0," * [1], "value of 1," * [2] ", value 2}Copy the code

Apply colours to a drawing

Template rendering iterates to find all template variables and then replaces them with matching variable values. If the template variable is not assigned a value, it is not replaced, but if the template variable is wrapped in curly braces' {} ', it is replaced with an empty string.Copy the code

Dynamic SQL

If you have the pain of concatenating different SQL statements according to different conditions, use ZSTR dynamic SQL once and for all

The sample

zstr.SqlRender("select * from table where &a (&b |c)".map[string]interface{} {"a": 1."b": 2."c": 3,})Copy the code

Template syntax Description

Syntax format is as follows: (operator) (variable name) {(operator) (variable name)} {(operator) (variable name) (tag)} {(operator) (variable name) (marking) (option)} {(operator) (variable name) (option)} example: &a {&a} {&a like} {&a like d} {&a d}Copy the code

The operator

  • The & is converted to and to mark the value

  • | to the or variable name tag values

  • # to value

    Built-in Attention optionCopy the code
  • @ is converted to a value, usually used to write a statement

    The attention option has no direct option and does not enclose the string in quotesCopy the code

The variable name

Template variables start with an operator. Variable names are supported: underscore; Upper and lower case letters; Digital; The decimal point template variable can be wrapped in curly braces' {} 'to accurately define the beginning and end of the template variable. Curly braces are allowed within the space example: & a | a_b # A.c {& a... c} {@9} @0... ACopy the code

tag

The default flag is =

>
>=
<
<=
!=  <>
=
in
not_in  notin
like
like_start  likestart
like_end  likeend
Copy the code

options

  • Attention, the syntax A, does not ignore zero values of that type
  • Direct, syntax D, writes values directly to SQL statements
  • Must, the syntax m, must pass

Template Rendering instructions

func SqlRender(sql_template string, values ...interface{}) string
// sql_template represents the SQL to render
// values indicates the variable value
Copy the code

Variable name matching

The variable name is subscript, and the subscript starts from 0. For example, A [0] represents the first occurrence of A, and a[5] represents the sixth occurrence of A. Equal to the variable name, for example: a; a_b; A.c; a.... c; 9. 0... A The asterisk * can match any variable name, but it must be used with A subscript that starts at 0. For example, *[0] indicates one variable, and *[5] indicates the sixth variable.Copy the code

Variable names match priorities

  1. Variable names with subscripts
  2. The variable name
  3. Asterisks with subscripts

value

Reference MakeMapOfValues

Any type of map is supported, for example, map[string]interface{}; The map (int) int, map [string] int, etc. Preach and support order value, such as Render (" to Render the text ", value of 0, 1, 2) value, it will be converted to the following description map [string] interface {} {" * [0] ": the value of 0," * [1], "value of 1," * [2] ", value 2}Copy the code

Apply colours to a drawing

All Spaces of the text are indented before rendering the text, so the text to be rendered is generally a pure SQL statement. Instead of writing values in the text, use the concept of passing parameters, which is a standard posture for using SQL

Template rendering iterates through all of the template syntax and then replaces it with different values. In general, if the variable has no arguments or a zero value of that type, it is replaced with an empty string. If the value of the variable is nil, different flags are converted into different statements.Copy the code

Template rendering and dynamic SQL performance description

We've written a special function to replace the regular lookup rule. Benchmark tests show that the speed of the regular lookup variable is 230%. For specific performance, clone the code and run the following command go test-v-bench.Copy the code