feat: use generics in function e2s
This commit is contained in:
parent
6adb2bb175
commit
d15e6106ee
22
main.go
22
main.go
@ -18,10 +18,14 @@ type User struct {
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
||||||
f, _ := excelize.OpenFile("Book1.xlsx")
|
data := excelToStruct[User]("Book1.xlsx", "Sheet1")
|
||||||
rows, _ := f.GetRows("Sheet1")
|
|
||||||
|
|
||||||
var dataExcel []User
|
fmt.Println(data)
|
||||||
|
}
|
||||||
|
|
||||||
|
func excelToStruct[T any](bookPath, sheetName string) (dataExcel []T) {
|
||||||
|
f, _ := excelize.OpenFile(bookPath)
|
||||||
|
rows, _ := f.GetRows(sheetName)
|
||||||
|
|
||||||
firstRow := map[string]int{}
|
firstRow := map[string]int{}
|
||||||
|
|
||||||
@ -29,11 +33,11 @@ func main() {
|
|||||||
firstRow[row] = i
|
firstRow[row] = i
|
||||||
}
|
}
|
||||||
|
|
||||||
dataExcel = make([]User, 0, len(rows)-1)
|
t := new(T)
|
||||||
|
dataExcel = make([]T, 0, len(rows)-1)
|
||||||
|
|
||||||
for _, row := range rows[1:3] {
|
for _, row := range rows[1:3] {
|
||||||
u := User{}
|
v := reflect.ValueOf(t)
|
||||||
v := reflect.ValueOf(&u)
|
|
||||||
if v.Kind() == reflect.Pointer {
|
if v.Kind() == reflect.Pointer {
|
||||||
v = v.Elem()
|
v = v.Elem()
|
||||||
}
|
}
|
||||||
@ -52,13 +56,13 @@ func main() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
dataExcel = append(dataExcel, u)
|
dataExcel = append(dataExcel, *t)
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Println(dataExcel)
|
return dataExcel
|
||||||
}
|
}
|
||||||
|
|
||||||
func convertType(objType string, value string) interface{} {
|
func convertType(objType string, value string) any {
|
||||||
switch objType {
|
switch objType {
|
||||||
case "int":
|
case "int":
|
||||||
valueInt, _ := strconv.Atoi(value)
|
valueInt, _ := strconv.Atoi(value)
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user