Setting breakpoints for Golang

Since sst do not support golang setting breakpoints yet, I need a way to debugging golang.

  1. install dlv
  2. setting launch.json
{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Debug Go Handler",
      "type": "go",
      "debugAdapter": "dlv-dap", 
      "request": "attach",
      "mode": "remote",
      "port": 2345,
      "host": "127.0.0.1", 
      "substitutePath": [
        {
          "from": "${workspaceFolder}/functions/lambda",
          "to": "${workspaceFolder}/functions/lambda"
        },
      ]
    },
    {
      "name": "Debug SST Dev",
      "type": "node",
      "request": "launch",
      "runtimeExecutable": "${workspaceRoot}/node_modules/.bin/sst",
      "runtimeArgs": [
        "dev",
        "--increase-timeout"
      ],
      "console": "integratedTerminal",
      "skipFiles": [
        "<node_internals>/**"
      ],
      "env": {}
    }
  ]
}
  1. replace node_modules/sst/runtime/handlers/go.js for it’s line 28, inside startWorker function
...
            const proc = spawn( 
                "dlv",
            [
                "exec",
                "--listen=:2345",
                "--headless",
                "--api-version=2",
                path.join(input.out, handlerName)
            ],
            {
                env: {
                    ...process.env,
                    ...input.environment,
                    IS_LOCAL: "true",
                    AWS_LAMBDA_RUNTIME_API: `localhost:${server.port}/${input.workerID}`,
                },
                cwd: input.out,
            });
...
  1. run Debug SST Dev configuration, and then trigger lambda, it will hold the process at port 2345 waiting for client connect, run Debug Go Handler to connect