func StructToMap(s interface{}) map[string]interface{} {
    return rawStructToMap(s, false)
}
convert struct to map
package utils
import (
    "encoding/json"
    "errors"
    "fmt"
    "io/ioutil"
    "os"
    "reflect"
    "regexp"
)
func NamedRegexpGroup(str string, reg *regexp.Regexp) (ng map[string]string, matched bool) {
    rst := reg.FindStringSubmatch(str)
    if len(rst) < 1 {
        return
    }
    ng = make(map[string]string)
    lenRst := len(rst)
    sn := reg.SubexpNames()
    for k, v := range sn {
        if k == 0 || v == "" {
            continue
        }
        if k+1 > lenRst {
            break
        }
        ng[v] = rst[k]
    }
    matched = true
    return
}
func FileExists(path_ string) (bool, error) {
    _, err := os.Stat(path_)
    if err == nil {
        return true, nil
    }
    if os.IsNotExist(err) {
        return false, nil
    }
    return false, err
}
func SnakeCasedName(name string) string {
    newstr := make([]rune, 0)
    firstTime := true
    for _, chr := range name {
        if isUpper := 'A' <= chr && chr <= 'Z'; isUpper {
            if firstTime == true {
                firstTime = false
            } else {
                newstr = append(newstr, '_')
            }
            chr -= ('A' - 'a')
        }
        newstr = append(newstr, chr)
    }
    return string(newstr)
}
func MapToStruct(m map[string]interface{}, s interface{}) {
    v := reflect.Indirect(reflect.ValueOf(s))
    for i := 0; i < v.NumField(); i++ {
        key := v.Type().Field(i).Name
        v.Field(i).Set(reflect.ValueOf(m[key]))
    }
}
func rawStructToMap(s interface{}, snakeCasedKey bool) map[string]interface{} {
    v := reflect.ValueOf(s)
    for v.Kind() == reflect.Ptr {
        v = v.Elem()
    }
    if v.Kind() != reflect.Struct {
        panic(fmt.Sprintf("param s must be struct, but got %s", s))
    }
    m := make(map[string]interface{})
    for i := 0; i < v.NumField(); i++ {
        key := v.Type().Field(i).Name
        if snakeCasedKey {
            key = SnakeCasedName(key)
        }
        val := v.Field(i).Interface()
        m[key] = val
    }
    return m
}