Home
Support Forums

Unable to create Go Function

I am unable to clone template GitHub - netlify/aws-lambda-go-example to create a go function. It gives error downloading from URL.

Welcome to the forums @Wanderer

How are you trying to clone the template? I just clone it.

I am using Netlify CLI with an option to specify a github URL for the function template.

All I want to do is create a function in golang.
I already have my go file in place, but when I push my code it gets deployed but the function doesn’t get created.

Hi @Wanderer,

Could you share the Site ID or name?

@hrishikesh

Here is the site name “youthful-cori-3f2024”
I am basically creating a function using a random JS template using CLI (because cloning github repository is not working)
Then deleting the js file and creating the go file in the function folder
then committing and pushing which triggers the deploy

no function is getting created

Can you try publishing the hello.go function from the docs? Build serverless functions with Go | Netlify Docs

Done… hello.go pushed to the project… no function created

https://app.netlify.com/sites/youthful-cori-3f2024/deploys/61043237d3bf26000803083d#L57

That line says no functions were found in the server directory. Are you sure it’s the correct folder?

The screenshot shows the content of /server folder in my project.
Normally it should reflect 4 functions… no?
Each folder contains the function’s code

image

I believe, for Go, you should directly have the file hello.go and not hello/index.go or something like that. I’ve not tested it myself, but that could be it. Would you give that a go?

After searching, some say that it should rather be a binary file in the functions directory.
I made few changes, now the functions directory is “/functions”
And the screenshot shows the content of this directory, with the go file built into a binary exe file.
After pushing to the repository, still the function not created.

I hope I could get some assistance… I am new to netlify and it seems to be a promising platform

image

Hi @Wanderer,

You’re building exe, that won’t work. Here’s an example repo: GitHub - netlify/aws-lambda-go-example.

@hrishikesh

Thanks a lot… I have worked it out. I will write a full post later to make this easier for later comers.
But I just need one last assistance… if you could guide me through how to access environment variables in a golang function.

Much appreciated

Hi @Wanderer,

Does os.Getenv(key) not work?

@hrishikesh

I need to know how to import the os variable

Do you have any related documentation link available?

import "os"

or

import (
    "other-package"
    "os"
)

Is that what you’re asking?

There’s no documentation as such, but we can improve it if we can figure out what exactly it is that you need.

For what it’s worth, I wrote a guide to creating functions in Go: How to Use Netlify to Deploy a Free Go Web Application · The Ethically-Trained Programmer

It sounds like you’re building the Go binary locally in Windows, but it should be built server side by Netlify in Linux.

1 Like

@carlmjohnson

Thanks a lot… that was very helpful

However, it’s not obvious what the endpoint will be in this case.

We have the endpoint of the Netlify function, and the “/api/feed” that you exposed.

How to resolve the proper endpoint to call?

Hi @Wanderer,

I am not sure if I understand your question. The endpoint will always be /.netlify/functions/function-name. it’s just the rewrite that’s acting as a proxy to make it available at /api/.

So if you’ve an endpoint at /.netlify/functions/foo, the redirect rule added in the blog post would let you write your JS as: /api/foo/.

Hello @hrishikesh

So here there are two things in play: the function name and the redirect rule.
What does the code exposing an endpoint have to do with them?
Does it mean here that the function should be named “feed”?

http.Handle("/api/feed",...