gpt4 book ai didi

excel - 如何在golang函数中返回结构字典

转载 作者:数据小太阳 更新时间:2023-10-29 03:44:49 25 4
gpt4 key购买 nike

我需要从函数中重新运行 struct duitonary,当它运行脚本时,我开始无法在返回参数中使用 res(类型 []exceldata)作为类型 []struct {}

我已经在我的 go 脚本中创建了 struct,我向它添加了值并添加到数组中,现在我需要将它返回到主要函数中


package main

import (
"fmt"
"database/sql"
_ "github.com/go-sql-driver/mysql"
"github.com/360EntSecGroup-Skylar/excelize"
"log"

)


type exceldata struct {
username string
rfid string
user string
}

func read() []struct{} {
exdata := exceldata{}
res := []exceldata{}
f, err := excelize.OpenFile("./required_details.xlsx")
if err != nil {
fmt.Println(err)
return res
}

// Get value from cell by given worksheet name and axis.
/*cell, err := f.GetCellValue("Sheet1", "A566")
if err != nil {
fmt.Println(err)
return
}
fmt.Println(cell)*/

// Get all the rows in the Sheet1.
rows, err := f.GetRows("Sheet1")

for _, row := range rows {
if row[0] != "eof"{

exdata.username = row[0]
exdata.rfid = row[1]
exdata.user = row[2]
res = append(res, exdata)
fmt.Println(res)

}else{
return res
}
}
return res;

}

func main() {

fmt.Println("Go MySQL Tutorial")
resexceldata := []exceldata{}
resexceldata =read()
fmt.Println("Routes are Loded.")

}

最佳答案

您已经将 excel 数据定义为一种类型,因此您应该使用该类型:

type exceldata struct
...


func read() []exceldata {
...
}

关于excel - 如何在golang函数中返回结构字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55931971/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com