title: Return nil for a struct in Go
body: <p>I am connecting to a database, getting a row and then sending it back to the user. What I want to do though is have a <code>return</code> statement if I can't find that row or if I have an error.</p>
  
  <p>Because I'm returning a struct I can't return nil and I get that error <code>cannot use nil as type Item in return argument</code> (Item is my struct)</p>
  
  <p>I read online that if I use a pointer in the return statement and return *Item instead of Item then I'd be able to pass nil, when I try to create <code>item := *Item{}</code> I get the following error <code>invalid indirect of Item literal (type Item)</code></p>
  
  <p>I think there are a few solutions to this but I can't find any, what I really want to know is:</p>
  
  <ul>
  <li>How to return a pointer *Item instead of Item</li>
  <li>Is there another way to return nil for a struct?</li>
  </ul>
  
  <p>Here's my code:</p>
  
  <pre><code>package main
  
  import (
      "fmt"
      "github.com/aws/aws-lambda-go/lambda"
  
    "github.com/aws/aws-sdk-go/aws"
    "github.com/aws/aws-sdk-go/aws/session"
    "github.com/aws/aws-sdk-go/service/dynamodb"
    "github.com/aws/aws-sdk-go/service/dynamodb/dynamodbattribute"
  )
  
  type Request struct {
      Name string `json:"name"`
  }
  
  type Item struct {
    Name string `json:"name"`
    Stock int `json:"stock"`
    Price float64 `json:"price"`
  }
  
  func Handler(request Request) (Item, error) {
    sess, err := session.NewSession(&amp;aws.Config{
      Region: aws.String("us-west-2")},
    )
  
    // Create DynamoDB client
    svc := dynamodb.New(sess)
  
    result, err := svc.GetItem(&amp;dynamodb.GetItemInput{
      TableName: aws.String("Inventory"),
      Key: map[string]*dynamodb.AttributeValue{
        "name": {
            S: aws.String(request.Name),
        },
      },
    })
  
    if err != nil {
      fmt.Println(err.Error())
  //     return nil, err
    }
  
    item := Item{}
  
    err = dynamodbattribute.UnmarshalMap(result.Item, &amp;item)
  
    if err != nil {
      panic(fmt.Sprintf("Failed to unmarshal Record, %v", err))
  //     return nil, err
    }
  
    if item.Name == "" {
        fmt.Println("Could not find item")
  //       return nil, nil
    }
  
    fmt.Println("Found item:")
    fmt.Println("Name:  ", item.Name)
    fmt.Println("Stock: ", item.Stock)
    fmt.Println("Price:  ", item.Price)
  
      return item, nil
  }
  
  func main() {
      lambda.Start(Handler)
  }
  </code></pre>