Working with Go functions locally and in deployment

I have a simple react site that is going to send a request to Square. The React portion of the form submits CC data, etc… but needs a server side bit of code in Go to then submit to the Square API. I am trying to get a Go function working locally, as well as deployed on netlify.

First, I have my GO configured per the instructions. However, when including a .toml file, it caused my netlify build to NOT build the site… basically overrode the site settings via Dashboard. Makes sense, except nothing I found in documentation indicated how you build both the react/nodejs site AND the go functions. That combo seems to not be documented well with regards to how you get netlify to build both without the Go .toml config causing the nodejs react bits to not build/deploy.

In the .toml, there is the command = “make build”. So what I tried was adding the Makefile and adding in the npm run build step, which I think is what is needed. This builds the Go function AND the nodejs, and it does deploy. I also see for my site it says one lambda function deployed, and the log shows invoked. Though the response I get back is a 429 Too many requests via sentry.io. I am not sure why this is, or how to solve this one.

The bigger issue, besides better documentation around deploying a go function AND a nodejs site together, is how to run the Go function locally. There is info on doing this for nodejs, but nothing on how you run the Go lambda locally so that you can test the site making a call to the local Go function.

So, how do we do this? Is the Go function a standalone server, and I just execute it after I build it via the ./goFunction?? When I do run it like this, it seems to just sit there… so not sure if it is actually running like a server. If so, I am not entirely sure how to access it. In my React site, I use the /.netlify/functions/goFunc in a link or button or in my case, the Square form handler code then invokes the /.netlify/functions/goFunc using the fetch() library call.

It would be ideal if there was a tutorial on how you create a react site that makes a call to a Go function and how to do test it locally then see it deploy to the site.

Any info on anyone who has gotten this to work would be fantastic.

Thank you.

Hi @jus,

Thanks for reaching out. I understand your frustration. I wish there was an easier path for Go functions, but Go is different since it generates a binary and the lambda handler function essentially acts as an RPC server in the function. Testing it locally won’t work easily because you can’t call it the same way you can a Javascript lambda function. You’d have to use a docker image or something similar. I don’t do this but you can find some info here.

I make heavy use of Go functions, and I personally don’t use the make file, but you of course can. I instead put the commands in npm scripts in a package.json like you can see in this repo: functions-react-mongo/package.json at master · futuregerald/functions-react-mongo · GitHub

Just to be clear, there are multiple ways of building a go function, you can chain the build function command and build site with && since the build command is just a bash shell, and you’re free to use it as such.

In terms of the local story with Go functions, as I said, it’s not easy to run them locally so you’ll end up doing something like running it in Docker. What I typically do is test them in branch deploys and also test the individual parts of the function separately. If you minimize the logic in the actual handler function and instead have other methods that are easily testable, then that would help you make sure your code is working as intended.

Hi. Thank you for answering. I am fine with running docker locally. I actually started down that path to run go lambda in a docker container, though still not able to make it work. What I decided to do was look at running the go server bit as an API endpoint and look to deploy that as a container in the cloud. Assuming this works out, the path to call the API would replace my client side app code that calls /.netlify/.

As this is for integrating payment processors, I am also waiting on some details from a couple of them on if there is a client side only SDK that can be used vs the server side component, which at least for Square, indicated a server side component was necessary… and is why I started looking at using the functions of netlify. Today I believe it is easy/cheap enough to deploy a simple API endpoint within a container to the cloud, as well as run it locally… though ensuring it is able to handle production level use (e.g. load balancing, scaling, etc… should it be needed) is a problem I hope to have one day :D. Again thank you for the info. I think I am going to skip the idea of using go functions until it is better fleshed out by the netlify team and/or examples of how to get it working for local dev are available.

Thank you && Good luck, Jus! Hope you find a solution that works for you.

Sorry that this is old thread. Before I jump down rabbit hole, just want to confirm that the Netlify Dev CLI tool doesn’t support Go Lambda functions, correct?

Hi, @carl, that is correct. The Netlify CLI tool (aka netlify dev) will allow for local testing of javascript functions/lambdas but not functions/lambdas written in Go.

For future reference, here is a blog post about writing a Go app that works both in Netlify and locally: How to Use Netlify to Deploy a Free Go Web Application · The Ethically-Trained Programmer

2 Likes