您好,可以使用第三方库excelize来读取excel中的数据,并使用反射来动态生成golang的struct结构。以下是示例代码: ``` package main import ( "fmt" "github.com/360EntSecGroup-Skylar/excelize" "reflect" ) func main() { f, err := excelize.OpenFile("test.xlsx") if err != nil { fmt.Println(err) return } sheetName := "Sheet1" rows, err := f.GetRows(sheetName) if err != nil { fmt.Println(err) return } structName := "TestStruct" structFields := make(map[string]string) for i, row := range rows { if i == { for _, cell := range row { structFields[cell] = "string" } } else { break } } structType := reflect.StructOf([]reflect.StructField{}) structValue := reflect.New(structType).Elem() for fieldName, fieldType := range structFields { field := reflect.StructField{ Name: fieldName, Type: reflect.TypeOf(fieldType), } structType = reflect.StructOf(append(structType.Fields(), field)) structValue.FieldByName(fieldName).Set(reflect.Zero(reflect.TypeOf(fieldType))) } fmt.Printf("type %s struct {\n", structName) for fieldName, fieldType := range structFields { fmt.Printf("\t%s %s\n", fieldName, fieldType) } fmt.Println("}") // Output: // type TestStruct struct { // Name string // Age string // Gender string // } } ``` 以上代码可以读取excel中的第一行作为struct的字段名,动态生成struct结构,并输出struct的定义。