Multiple Endpoints in Go on Netlify Functions

Share my case to fill the lack of the official documentation

Kengo Hamasaki / hmsk
Hai-iro
3 min readFeb 17, 2020

--

I picked Go for my new programming language for 2020. I touched a bit before but didn’t have enough time to write a solid thing with Go until this time. After reading through the official documents, I decided to replace my subtle tool which I had written by CoffeeScript with Go.

The tool is Hubot Script to have endpoints to accept HTTP requests on Heroku to run some tasks like notifying to Slack team for my family.

The major use case is “Notifying summarized events from the alert for our credit-cards usage”. Receiving email notifications on Mailgun, and Mailgun makes an HTTP request into the Hubot host. Then a Hubot Script parses email and notifies to Slack.

From #finance channel on our Slack team

Chose the FaaS service Netlify Functions (Actually, Netlify is wrapping AWS Lambda btw) to run those tasks. NF supports JS and Go, we can trigger functions by HTTP req, and functions could be beside of source codes for a website on Netlify. My recent projects are mostly running on Netlify, so I intended to get rid of the exception which runs on Heroku. (Feeling hard for remembering the operations to handle the app on Heroku for just maintaining the scripts 🙃)

However, I guess Netlify really doesn’t recommend Go lang for using on Netlify Functions since their document and sample codes are not well-documented and their dashboard shows as “.js” for each function 👀

*.js 🤔

If there are multiple binaries on “functions” directory, Netlify deploys each like JS functions. Only the thing Netlify miss is their sample doesn’t suppose their user may want to have multiple endpoints.

netlify-functions-in-go boilerplate

As a newbie for Go lang, I concluded that the doc didn’t guide me into what I build. Learned a bit about file structures of typical Go project, how to build multiple binaries and configured some Netlify settings. This repo is the outcome of my experiments. You can deploy a boilerplate (and create a repo on your GitHub account) by just hitting “Deploy to netlify” button 😉

The boilerplate gives 2 endpoints which return a message:

Each endpoint runs a compiled binary of /cmd/[function name]/main.go. If you’d like to have common codes for multiple functions, place /internal/pkg/[name]/[name].go and import that.

See README for more details.

--

--