How to map path parameters in golang?

Context

I am trying to build a Go Serverless function with vercel, but my route needs a path parameter.

The Problem

How can I map path parameters in golang with vercel?

Attempts

  1. Given the following folder structure
  • api
    • user
      if i use code like
func GetUser(w http.ResponseWriter, r *http.Request) {
	usr := strings.TrimPrefix(r.URL.Path, "api/user/")
	if cal == "" {
		http.Error(w, "Invalid user", http.StatusBadRequest)
		return
	}
	_, err := w.Write([]byte(cal))
	if err != nil {
		slog.Error("Error answering", err)
	}
}

But I when I make a request to /api/user/1 it returns a 404 (not found)

  1. Same but with [id] directory
  2. Same but with [id].go file

There’s another community post with 404 debugging tips that might be helpful. Please give these solutions a try and let us know how it goes.

A human should be around soon to offer more advice. But you can also get helpful information quickly by asking v0.

for the moment, I’m limited to query parmeters

Hi @gusos-admin, welcome to the Vercel Community!

I think you can create a rewrites from /api/user/(.*) to api/user.go file using the vercel.json. This way you can rewrite all incoming requests to a certain route and handle the path variables.

  "rewrites": [{ "source": "api/user/(.*)", "destination": "/api/user.go" }],

I hope this helps.