title: Specify multiple event handlers for AWS Lambda in Go
body: <p>Typically, a AWS Lambda event handler code in Go (using Serverless Framework) is coded as:</p>
  
  <pre class="lang-golang prettyprint-override"><code>package main
  
  import (
    "fmt"
    "context"
    "github.com/aws/aws-lambda-go/lambda"
  )
  
  type MyEvent struct {
    Name string `json:"name"`
  }
  
  func HandleRequest(ctx context.Context, name MyEvent) (string, error) {
    return fmt.Sprintf("Hello %s!", name.Name ), nil
  }
  
  func main() {
    lambda.Start(HandleRequest)
  }
  </code></pre>
  
  <p>The <code>serverless.yml</code> file then contains a section like:</p>
  
  <pre><code>  skeleton-go-get:
      name: skeleton-go-get
      runtime: go1.x
      handler: go-handler  # &lt;- This specifies a file, not a function.
      events:
        - http:
            path: skeleton/go
            method: get
  </code></pre>
  
  <p>That ^ creates one request handler... but now I want my one Go script / program to contain the event handlers for both HTTP GET and POST requests, and not use one Go program file per serverless function.</p>
  
  <p>Exactly this is possible in languages like Node.js, Ruby, Python, with the <code>serverless.yml</code> specifying which function in the handler file is to be used for which serverless function. For example (for Python functions):</p>
  
  <pre><code>[...]
  functions:
    skeleton-python-get:
      name: skeleton-python-get
      handler: python-handler.handle_get  # &lt;- Specifies the HTTP GET handler.
      events:
        - http:
            path: skeleton/python
            method: get
    skeleton-python-post:
      name: skeleton-python-post
      handler: python-handler.handle_post  # &lt;- Specifies the HTTP POST handler.
      events:
        - http:
            path: skeleton/python
            method: post
  </code></pre>
  
  <p>I cannot get this same trick to work for Go. I tried to include the proper request in <code>main()</code> but to no avail:</p>
  
  <pre class="lang-golang prettyprint-override"><code>func HandleGetRequest(ctx context.Context, name MyEvent) (string, error) {
    return fmt.Sprintf("Hello %s!", name.Name ), nil
  }
  
  func HandlePostRequest(ctx context.Context, name MyEvent) (string, error) {
    return fmt.Sprintf("Hello %s!", name.Name ), nil
  }
  
  func main() {
    lambda.Start(HandleGetRequest)
    lambda.Start(HandlePostRequest)  // &lt;- Attempt to add another handler.
  }
  </code></pre>
  
  <p>And specifying multiple event handler functions in the <code>serverless.yml</code> file for the Go handlers also doesn't work: the function isn't a valid part of the handler declaration.</p>
  
  <pre><code>  skeleton-go-get:
      name: skeleton-go-get
      runtime: go1.x
      handler: go-handler.HandleGet  # &lt;- Attempt to specify a function.
      events:
        - http:
            path: skeleton/go
            method: get
  
    skeleton-go-post:
      name: skeleton-go-post
      runtime: go1.x
      handler: go-handler.HandlePost  # &lt;- Attempt to specify a function.
      events:
        - http:
            path: skeleton/go
            method: post
  </code></pre>
  
  <p>Q: How can I include more than one AWS Lambda event handler in one Go program (using Serverless Framework)?</p>