登录
首页 >  Golang >  Go问答

如何在golang函数中返回结构化字典数据

来源:stackoverflow

时间:2024-03-05 21:30:20 432浏览 收藏

对于一个Golang开发者来说,牢固扎实的基础是十分重要的,golang学习网就来带大家一点点的掌握基础知识点。今天本篇文章带大家了解《如何在golang函数中返回结构化字典数据》,主要介绍了,希望对大家的知识积累有所帮助,快点收藏起来吧,否则需要时就找不到了!

问题内容

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

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

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.")

}


解决方案


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

type exceldata struct
...


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

文中关于的知识介绍,希望对你的学习有所帮助!若是受益匪浅,那就动动鼠标收藏这篇《如何在golang函数中返回结构化字典数据》文章吧,也可关注golang学习网公众号了解相关技术文章。

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>