Skip to content

Commit

Permalink
add Strutforscan
Browse files Browse the repository at this point in the history
  • Loading branch information
Allan Wei authored and Allan Wei committed Oct 22, 2018
1 parent 25b9223 commit 072192a
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package util
import (
"fmt"
"net/url"
"reflect"
"time"
)

Expand Down Expand Up @@ -122,3 +123,22 @@ func TimeParseRFC3339(timestr string) (time.Time, error) {
return v1, nil

}

//StrutForScan ...
func StrutForScan(u interface{}, columns []string) []interface{} {
val := reflect.ValueOf(u).Elem()
v := make([]interface{}, val.NumField())
typeOfT := val.Type()
for i := 0; i < len(columns); i++ {
for j := 0; j < val.NumField(); j++ {
valueField := val.Field(j)
name := typeOfT.Field(j).Name
if columns[i] == name {
v[i] = valueField.Addr().Interface()
break
}
}
}

return v
}

0 comments on commit 072192a

Please sign in to comment.