英文:
Get array of all plans of Stripe in Golang
问题
我正在尝试使用Stripe的Golang API获取我Stripe账户中存在的所有计划列表。根据这里提供的文档:https://stripe.com/docs/api/go#list_plans,它应该返回所有计划的列表。但是它只返回了一个计划的详细信息。
以下是我的代码:
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
}
有趣的是,我发现文档中提供的Golang的响应示例与其他语言(如PHP、Ruby等)有些不同。对于除了Go和.NET之外的语言,它返回一个计划数组,但对于Go和.NET,它只返回一个计划。所以我不确定这是API的默认行为还是某个bug。
任何形式的帮助都将不胜感激。
谢谢!
英文:
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
得分: 1
嘿,我找到了我的问题的答案:
我应该使用接口而不是结构实例:
func GetAllPlans() (interface{}) {
var plans []interface{}
params := &stripe.PlanListParams{}
it := plan.List(params)
for it.Next() {
plans = append(plans, it.Plan())
}
return plans
}
感谢 @u_mulder 提供的有用提示!
英文:
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!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论