Skip to content

Instantly share code, notes, and snippets.

@brandur
Last active May 21, 2018 17:35
Show Gist options
  • Save brandur/e1823806c5d1435c7c07782a50a10f02 to your computer and use it in GitHub Desktop.
Save brandur/e1823806c5d1435c7c07782a50a10f02 to your computer and use it in GitHub Desktop.
stripe-go API candidate #3: types with an interface, working
// This example is similar to candidate #2, but compiles because we make sure
// to define an alternate type for int64 (Int64) that we're allowed to add the
// Convert function to.
//
// This approach works, and is fairly clever, but ergonomically it looks very
// similar to the pointer approach in candidate #1. You use a type conversion
// instead of a function call when invoking Int64, but they look identical in
// code.
//
// Arguably, it has a small advantage over pointers in that because Int64 here
// is a type defined within our package, it can't be used interchangably with
// other packages. So for example, you couldn't extract an `*int64` from the
// AWS Go SDK and pipe it directly into Stripe -- you'd need to make an
// explicit conversion instead, which might help to prevent bugs. However, this
// is quite a minor advantage.
package main
import (
"strconv"
)
type Int64Convertible interface {
Convert() *string
}
type Int64 int64
func (v Int64) Convert() *string {
s := strconv.FormatInt(int64(v), 10)
return &s
}
type ChargeParams struct {
Amount Int64Convertible
}
func main() {
_ = ChargeParams{Amount: Int64(100)}
_ = ChargeParams{Amount: nil}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment