登录
首页 >  Golang >  Go问答

将结构数组作为函数参数进行传递

来源:stackoverflow

时间:2024-03-11 13:27:30 126浏览 收藏

有志者,事竟成!如果你在学习Golang,那么本文《将结构数组作为函数参数进行传递》,就很适合你!文章讲解的知识点主要包括,若是你对本文感兴趣,或者是想搞懂其中某个知识点,就请你继续往下看吧~

问题内容

我想使用 google cloud platform datastore 进行数据分页,并且我在 gcp 页面 (https://cloud.google.com/datastore/docs/concepts/queries) 上找到了使用游标进行数据分页的示例它工作得很好。

google 提供的示例已对变量 vartasks[]taskvartasktask 进行了硬编码,我想创建一个可重用函数,在其中我可以通过参数将指针传递给结构体数组输入 interface{} 并获取由该函数填充的结构。例如:

type mystruct1 struct {
        f1 string
    }

    type mystruct2 struct {
        f1 int
    }

    func list(ctx context.context, cursorstr string, data interface{}) {
    ...
    }

    func main() {
        mystruct1 := make([]mystruct1, 0)
        list(ctx, "", &mystruct1)

        mystruct2 := make([]mystruct2, 0)
        list(ctx, "", &mystruct2)
    }

当我需要在此函数中创建一个变量来存储记录,然后将其附加到作为指针传递的结构数组时,我的问题就开始了。

来自 google 的示例

func snippetiterator_cursor() {
    ctx := context.background()
    client, _ := datastore.newclient(ctx, "my-proj")
    cursorstr := ""
    // [start datastore_cursor_paging]
    const pagesize = 5
    query := datastore.newquery("tasks").limit(pagesize)
    if cursorstr != "" {
        cursor, err := datastore.decodecursor(cursorstr)
        if err != nil {
            log.fatalf("bad cursor %q: %v", cursorstr, err)
        }
        query = query.start(cursor)
    }

    // read the tasks.
    var tasks []task << this is what i want to be generic 
    var task task. << this is what i want to be generic 
    it := client.run(ctx, query)
    _, err := it.next(&task)
    for err == nil {
        tasks = append(tasks, task)
        _, err = it.next(&task)
    }
    if err != iterator.done {
        log.fatalf("failed fetching results: %v", err)
    }

    // get the cursor for the next page of results.
    nextcursor, err := it.cursor()
    // [end datastore_cursor_paging]
    _ = err        // check the error.
    _ = nextcursor // use nextcursor.string as the next page's token.
}

基于上面代码的我的通用函数

func list(ctx context.Context, kind string, data interface{}, pageSize int, cursorStr string) string {
    query := datastore.NewQuery(kind).Limit(pageSize)
    if cursorStr != "" {
        cursor, err := datastore.DecodeCursor(cursorStr)
        if err != nil {
            log.Fatalf("Bad cursor %q: %v", cursorStr, err)
        }
        query = query.Start(cursor)
    }
    it := query.Run(ctx)

    // HERE IS WHERE THE PROBLEM STARTS
    var vet []interface{}
    var rec interface{}

    k, err := it.Next(rec)
    if err != nil {
        log.Println(err.Error())
    }
    for err == nil {
        log.Println(k, rec) // PROBLEM: The key comes ok but rec comes nil
        vet = append(vet, rec)
        k, err = it.Next(rec)
    }
    log.Println(vet) // PROBLEM: vet has only nils

    nextCursor, err := it.Cursor()
    if err != nil {
        log.Println(err.Error())
    }

    data = vet
    return nextCursor.String()
}

func TestListFunc() {
    data := make([]Tasks, 0)
    cursor := list(ctx, "Tasks", &data, 10, "")
    x, _ := json.MarshalIndent(data, " ", "   ")
    log.Println(string(x))
}

问题:数据存储区迭代器 .next() 似乎没有将记录存储在类型为 interface{} 的变量中


解决方案


使用reflect包:

func list(ctx context.context, kind string, dst interface{}, pagesize int, cursorstr string) string {
    client, _ := datastore.newclient(ctx, "my-proj")
    query := datastore.newquery(kind).limit(pagesize)
    if cursorstr != "" {
        cursor, err := datastore.decodecursor(cursorstr)
        if err != nil {
            log.fatalf("bad cursor %q: %v", cursorstr, err)
        }
        query = query.start(cursor)
    }

    // get reflect value for the result slice.
    results := reflect.valueof(dst).elem()

    // allocate new value of the slice element type. 
    // resultp is pointer to that value.
    resultp := reflect.new(results.type().elem())

    it := client.run(ctx, query)
    _, err := it.next(resultp.interface())
    for err == nil {
        // append last value to results
        results.set(reflect.append(results, resultp.elem())

        _, err = it.next(resultp.interface())
    }
    if err != iterator.done {
        log.fatalf("failed fetching results: %v", err)
    }

    // get the cursor for the next page of results.
    nextcursor, err := it.cursor()
    // [end datastore_cursor_paging]
    _ = err        // check the error.
    _ = nextcursor // use nextcursor.string as the next page's token.
}

使用指向目标切片的指针调用该函数:

var data []Tasks
cursor := list(ctx, "Tasks", &data, 10, "")

以上就是本文的全部内容了,是否有顺利帮助你解决问题?若是能给你带来学习上的帮助,请大家多多支持golang学习网!更多关于Golang的相关知识,也可关注golang学习网公众号。

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