douwu7168 2017-08-23 07:34
浏览 57

获取Golang中Stripe所有计划的数组

I am trying to get list of all plans that exist in my Stripe account using Stripes's Golang API. As per documentation provided here: https://stripe.com/docs/api/go#list_plans it should return a list of all plans. But its returning me only a single plan details.

Here is my code:

package main

import (
    "github.com/gin-gonic/gin"
    "github.com/stripe/stripe-go"
    "github.com/stripe/stripe-go/plan"

)
func main(){
    router := gin.Default()
    stripe.Key = "stripe_api_key"

    router.GET("/plans", func(c *gin.Context) {
        plans := GetAllPlans()
        c.JSON(200, gin.H{  "plans": plans, })
    })
    router.Run(":8080")
}

func GetAllPlans() (plans *stripe.Plan){
    plans = &stripe.Plan{}
    params := &stripe.PlanListParams{}

    it := plan.List(params)
    for it.Next() {         
        plans = it.Plan()
    }
    return 
}

What's interesting I have found is the response example provided in the documentation for Golang is somewhat different from other languages like PHP, Ruby etc. For languages other than Go and .NET its returning an array of plans but for Go and .NET its returning a single plan. So I am not sure it is api's default behaviour or some bug.

Any kind of help will be appreciated.

Thanks!

  • 写回答

1条回答 默认 最新

  • douju4278 2017-08-23 10:21
    关注

    Hey I found the answer to my question:

    I should use a interface instead of structure instance:

    func GetAllPlans() (interface{}){
        var plans []interface{}
        params := &stripe.PlanListParams{}
    
        it := plan.List(params)
        for it.Next() {         
            plans = append(plans, it.Plan())
        }
        return plans
    }
    

    Thanks @u_mulder for a useful hint!

    评论

报告相同问题?

悬赏问题

  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程