Fn Easy Functions for Go

Reed Allman
Fn Project
Published in
2 min readOct 19, 2017

Since the Fn project is largely assembled by gophers, we decided to leave mounds all over your yard in an attempt to viciously murder your lawn mower. Err, I mean, we decided to add first class support for Go for Fn developers. One day we will conquer the lawn mowers. One day. For now, we are happy to release support for FDK-go, the Functions Development Kit for Go. FDK’s are purpose built for taking away the hassle of decoding and encoding input and output from the Fn server, so that developers only have to think about writing their functions.

If you’re already using Fn with Go functions that are using the “default” format, this should mean that with about 5 lines of code you can wrap up your existing functions using the fdk-go and get an easy 10x speedup by switching to using the “http” format (you can read about the magic here). If you’re using the “http” format already, you can probably delete 30 LOC and just import the fdk-go!

Here’s an example of going from “default” function to fdk-go:

Say you have a function using the “default” format with go code:

This code simply decodes json from standard in and checks for a name, giving one if not provided, and then spits out “hello” to that name on standard out (with gumption!). Pretty straight forward (p.s. tabs don’t work great in Medium, may gofmt have mercy on my soul).

Now let’s make it use fdk-go:

All we had to do was:

  • import "github.com/fnproject/fdk-go"
  • turn our previous main() into an fdk.Handler interface implementer
  • change os.Stdin and os.Stdout to in and out function parameters
  • call the fdk.Handle function with our new fdk.Handler in main()

Bam. Pretty easy to make the switch from “default” or to write new functions from scratch with fdk-go . Don’t forget when moving from “default” format to update your routes to use the “http” format, if you’re using the fdk-go the format will not matter and you can reap all the benefits of the “http” format.

If you want to run through an example using the fdk-go check out the examples in the repo, you’ll notice things are eerily similar, but the examples provide a Dockerfile that make it easy to upload your project to fn and run the function. It will only take 2 minutes!

Please give us feedback on the https://github.com/fnproject/fn or https://github.com/fnproject/fdk-go repos if there’s anything you’d like to see (or not see), or if you have any ideas on how to end the tyranny of the lawn mowers you can also find us on slack.

Have a great fn day!

--

--

Reed Allman
Fn Project

Hopelessly trying not to tie my identity to my job title