This go library returns a comma separated list of tags from pure structs.
The idea of this lib is to simplify SQL queries without using an ORM.
Example:
type Clients struct {
Name string `db:"username" json:"Username"`
City string `db:"location" json:"City"`
Age int `db:"age" json:"Age"`
Gender string `db:"mf" json:"Gender"`
Car string `db:"auto" json:"Auto"`
}
Instead of manually creating an SQL Query like "SELECT Name, City, Age, Gender, Car FROM..." the query can be written this way:
fmt.Sprintf("SELECT %v WHERE %v = @p1", list.StructTagList(Clients{},"db",true), field), param)
Whenever the struct changes automatically also the Query will change.
import "github.com/pitw/go-struct-tag-list/list"
The list package needs 3 params:
Param | Type | Example | Note |
---|---|---|---|
Struct | struct{} | Clients{} | The target struct |
Struct Tag | string | "db" | The struct tag which should be exported as list |
Check | bool | true | Checks if all fields in struct have this tag |
The list package returns a comma separated list as string and errors.
type Clients struct {
Name string `db:"username" json:"Username"`
City string `db:"location" json:"City"`
Age int `db:"age" json:"Age"`
Gender string `db:"mf" json:"Gender"`
Car string `db:"auto" json:"Auto"`
}
v, err := list.StructTagList(Clients{}, "db", true)
fmt.Print(v) // returns username,location,age,mf,auto
fmt.Print(err) // returns nil
v, err := list.StructTagTypeList(Clients{}, "db", true)