Skip to content

Instantly share code, notes, and snippets.

@alexyslozada
Created October 29, 2018 00:35
Show Gist options
  • Save alexyslozada/2e3c11c42978eddc098a9c51e09fe671 to your computer and use it in GitHub Desktop.
Save alexyslozada/2e3c11c42978eddc098a9c51e09fe671 to your computer and use it in GitHub Desktop.
Cómo leer un slice de datos de un flag
// Field structura de un tipo de campo del modelo
type Field struct {
Name string
Type string
NotNull string
}
// Helper estructura que permite procesar los campos digitados
type Helper struct {
Fields []Field
}
// String permite imprimir los campos
func (h *Helper) String() string {
return fmt.Sprint(h.Fields)
}
// Set permite distribuir los campos escritos en el flag
func (h *Helper) Set(value string) error {
fields := strings.Split(value, ",")
for _, v := range fields {
field := strings.Split(v, ":")
nn := "NOT NULL"
if len(field) == 3 {
if strings.ToLower(field[2]) == "f" {
nn = ""
}
}
f := Field{field[0], field[1], nn}
h.Fields = append(h.Fields, f)
}
return nil
}
// Modo de uso
// h := Helper{}
// flag.Var(&h, "fields", "nombre de los campos de la tabla y su tipo, separados por coma sin espacios (ej: name:string,phone:string,address:string,age:int)")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment