Earlier this year we switched development to Go. We’re pretty big fans of the language, initially developed at our cloud partners Google. Since Google App Engine supports Go, you can easily integrate SendGrid’s Go library on Google’s platform-as-a-service.

Getting Started

First, register an app in AppEngine for demo purposes. If you don’t have a SendGrid account, head on over to our Google Partnership page to find out more of the benefits. Modify your PATH to include the Go AppEngine binaries. After doing this, you shall have the goapp binary ready to run. To be sure you can run the following command: which goapp It should print out the location of the goapp binary.

Installing SendGrid

Using the Go package manager, you should just need one command to get the SendGrid library:

go get github.com/sendgrid/sendgrid-go

DONE! This will be available for all your future apps if you set up your GOPATH correctly.

Installing packages in Go is somewhat magical…You just install and there is no feedback.

If you are a bit more anal like me, and you like to have Version Numbering, you should check out Godep.

Create a Simple App

For this example I will use Martini, my favorite Go Web Framework. If you want to see how this would look like in any other framework (or simply vanilla Go) I’d be more than glad to show you. Just ping me on twitter.

So go ahead and run:

go get github.com/go-martini/martini

Now lets add some boilerplate code.

package demo import ( "appengine" "appengine/urlfetch" "github.com/go-martini/martini" "github.com/sendgrid/sendgrid-go" "net/http" ) func init() { sg := sendgrid.NewSendGridClient("SENDGRID_USERNAME", "SENDGRID_PASSWORD") m := martini.Classic() m.Get("/:email", func(r *http.Request, params martini.Params) string { context := appengine.NewContext(r) sg.Client = urlfetch.Client(context) email := sendgrid.NewMail() email.AddTo(params["email"]) email.SetSubject("Hello!") email.SetText("This was sent from your sample app!") email.SetFrom("yamil@sendgrid.com") email.SetFromName("Yamil @elbuo8") if e := sg.Send(email); e == nil { return "Sent!" } else { context.Infof("%v", e) return "Oups" } }) http.Handle("/", m) }

First of all, notice that the package is demo instead of main. That is because AppEngine already provides us with a main package. Then we initialize our SendGridClient by passing our credentials in.

Once that is done, using Martini’s simple routing options we create a simple route which will take in 1 parameter in the URL. This parameter should be an email since we are going to use it to be the recipient of the demo.

context := appengine.NewContext(r) sg.Client = urlfetch.Client(context)

AppEngine has the concept of Contexts. In order to perform an HTTP request on AppEngine’s infrastructure, a context needs to be created. Once you have such Context, you can perform logging, HTTP requests and much more. Since the SendGridClient needs to perform an HTTP

request, the HTTP client that it uses needs to be updated to the one using the Context. That’s where the urlfetch package comes in handy.

Deploying and Testing

Phew, we are almost at the finish line. We are going to need a app.yaml file for deployment.

Then, simply run:

goapp deploy

And your simple application will be compiled and uploaded! Head on over to your app and “test” it out!

If you’d like to get started with Go on something other than App Engine, it’s just as easy. I have a short tutorial to send email using the SendGrid Go library.