Multiple Endpoints in Go on Netlify Functions

Share my case to fill the lack of the official documentation

Kengo Hamasaki / hmsk
Feb 17 · 3 min read

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.

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 👀

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.

Hai-iro

A Japanese duo by a product designer and a software…

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store