Blog

Share this post on Twitter

Photo of Cosmin Nicolaescu

Official Go support

Cosmin Nicolaescu on September 23, 2014

We’re fans of Go (both the language and the game) here at Stripe, and it seems we’re not the only ones. In recent months, we’ve seen Go’s popularity rise amongst our users and more generally in the open-source community, so we decided to add an official Stripe library for Go.

Requests made to Stripe using Go in 2014

We’ve also started using more Go at Stripe internally. For example, parts of the system that power Checkout are built in Go (and use this library). When porting some existing services to Go, we’ve noticed 2-4x increases in throughput (and our engineers were pretty happy with the development process).

To get started with our Go library, go get github.com/stripe/stripe-go and then import it in your code. Here’s how you’d create a charge:

import (
  "github.com/stripe/stripe-go"
  "github.com/stripe/stripe-go/currency"
)

params := &stripe.ChargeParams {
    Amount:   1000,
    Currency: currency.USD,
    Card:     &stripe.CardParams{Token:"tok_mastercard"},
    Desc:     "Gopher t-shirt",
}

There are two ways to make calls with the library, based on your needs. The simplest way is to use the global implicit client and invoke the APIs:

import (
  "github.com/stripe/stripe-go/charge"
)

stripe.Key = "tGN0bIwXnHdwOa85VABjPdSn8nWY7G7I"
ch, err := charge.New(params)

If your scenario involves concurrent calls or you’re dealing with multiple API keys, you can use an explicit client:

import (
  "github.com/stripe/stripe-go/client"
)

sc := &client.Api{}
sc.Init("tGN0bIwXnHdwOa85VABjPdSn8nWY7G7I", nil)

ch, err := sc.Charges.New(params)

Given Go’s lack of built-in versioning, we highly recommend you use a package management tool to avoid any unforeseen upgrades.

The library features iterator-based listing, which handles pagination for you automatically. We’ve also added support for injecting mocks to make testing easier. And if you need more control, the library allows you to inject your ownhttpClient for transport-level customizations.

Check out the docs (or the GoDoc) for more details and examples. Let me know if you have any feedback, or send a pull request my way!