在Go中为结构返回nil

Nag*_*hab 6 go

我正在连接到数据库,获取一行,然后将其发送回用户。但是,return如果找不到该行或出现错误,我想做的就是声明。

因为我要返回一个结构,所以我不能返回nil,但得到了这个错误cannot use nil as type Item in return argument(Item是我的结构)

我在网上阅读到,如果我在return语句中使用指针并返回* Item而不是Item,那么我可以传递nil,当我尝试创建时item := *Item{}出现以下错误invalid indirect of Item literal (type Item)

我认为有一些解决方案,但我找不到,我真正想知道的是:

  • 如何返回指针* Item而不是Item
  • 还有另一种方法可以为结构返回nil吗?

这是我的代码:

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(&aws.Config{
    Region: aws.String("us-west-2")},
  )

  // Create DynamoDB client
  svc := dynamodb.New(sess)

  result, err := svc.GetItem(&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, &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)
}
Run Code Online (Sandbox Code Playgroud)

Eli*_*gem 11

您分配的item变量错误。您说过您尝试过item := *Item{},而创建指针的方法要么是通过使用new内置函数,要么是创建文字,然后是地址运算符(&)。后者是您在golang中最常看到的方法。在某些情况下会使用new,但是在这种情况下,我会选择第二种方法:

所以:

item := &Item{}
// or
item := new(Item)
Run Code Online (Sandbox Code Playgroud)

最后,您可以按原样保留代码,并只在末尾返回一个指针:

item := Item{}
// some code here
return &item, nil
Run Code Online (Sandbox Code Playgroud)

万一您必须返回错误,您仍然可以 return nil, err

因此,将所有内容放在一起:

// return *Item instead of Item
func Handler(request Request) (*Item, error) {
   // your code here, eg:
   item := Item{}
   if err := dynamodbattribute.UnmarshalMap(result.Item, &item); err != nil {
        return nil, err
    }
    return &item, nil
}
Run Code Online (Sandbox Code Playgroud)

或者,item从头开始分配为指针

func Handler(request Request) (*Item, error) {
   // your code here, eg:
   item := &Item{}
   if err := dynamodbattribute.UnmarshalMap(result.Item, item); err != nil {
        return nil, err
    }
    return item, nil
}
Run Code Online (Sandbox Code Playgroud)

  • 这可能是显而易见的,但只是为了确保 OP 也应该将“Handler”声明中的返回类型从“Item”更新为“*Item”。 (2认同)
  • @RylanSchaeffer 因为 `item` 作为参数传递给 `UnmarshalMap`。如果你不声明它,你就不能使用它(即将它作为参数传递)。在使用变量之前您需要它。如果除非需要,否则不想分配任何内存,只需将 `item := Item{}` 替换为 `var item Item`,将其作为 `&item` 传递给 `UnmarshalMap` 调用,然后返回 `&item ` 表示成功,`nil` 表示错误。`var item Item` 不会分配对象,除非需要它,它在语言规范中的某个地方提到过。 (2认同)