0

I'm trying to import "github.com/aws/aws-sdk-go-v2/feature/dynamodb/attributevalue" for my main.go file for my lambda function but every time I save the import disappears.

I have some simple Golang code trying to update a visitor count by updating a dynamodb table.

The build keeps failing saying that attributevalue are undefined but I can't save the import for attribute value.

package main

import (
    "context"
    "log"

    "github.com/aws/aws-lambda-go/events"
    "github.com/aws/aws-lambda-go/lambda"

    "github.com/aws/aws-sdk-go-v2/aws"
    "github.com/aws/aws-sdk-go-v2/config"
    "github.com/aws/aws-sdk-go-v2/feature/dynamodb"
    "github.com/aws/aws-sdk-go-v2/service/dynamodb"
)

func handler(request events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {

    cfg, err := config.LoadDefaultConfig(context.TODO(), config.WithRegion("us-east-1"))
    if err != nil {
        log.Fatalf("unable to load SDK config, %v", err)
    }

    svc := dynamodb.NewFromConfig(cfg)

    // Build the request with its input parameters
    resp, err := svc.UpdateItem(context.TODO(), &dynamodb.UpdateItemInput{
        TableName: aws.String("table-name"),
        Key: map[string]*dynamodb.attributevalue{
            "ID": {
                S: aws.String("visitors"),
            },
        },
        UpdateExpression: aws.String("ADD visitors :inc"),
        ExpressionAttributeValues: map[string]*dynamodb.attributevalue{
            ":inc": {
                N: aws.String("1"),
            },
        },
    })

    if err != nil {
        log.Fatalf("Got error callingUpdateItem: %s", err)
    }

    return events.APIGatewayProxyResponse{
        Headers: map[string]string{
            "Access-Control-Allow-Origin":  "*",
            "Access-Control-Allow-Methods": "*",
            "Access-Control-Allow-Headers": "*",
        },
        StatusCode: 200,
    }, nil
}

func main() {
    lambda.Start(handler)
}

When I do go mod vendor I get

github.com/aws/aws-sdk-go-v2/featrues/dynamodb: no required module provides package github.com/aws/aws-sdk-go-v2/featrues/dynamodb; to add it:

Doing go get github.com/aws/aws-sdk-go-v2/featrues/dynamodb I get

go: module github.com/aws/aws-sdk-go-v2@upgrade found (v1.16.11), but does not contain package github.com/aws/aws-sdk-go-v2/featrues/dynamodb

I am very new to Go and have no idea what to do to resolve this. Any help would be greatly appreciated.

echough1
  • 1
  • 1
  • looks like you have a typo. Import should be for "github.com/aws/aws-sdk-go-v2/feature/dynamodb/" and not "github.com/aws/aws-sdk-go-v2/featrues/dynamodb" – Manoj Guglani Aug 15 '22 at 18:08
  • Ty for pointing out the typo I did not see that but I'm still getting the same errors – echough1 Aug 15 '22 at 18:17
  • if you check github.com/aws/aws-sdk-go-v2/feature. available packages are dynamodb/attributevalue or dynamodb/expression. Looks like you are using attribute value so the import should have `"github.com/aws/aws-sdk-go-v2/feature/dynamodb/attributevalue"` – Manoj Guglani Aug 15 '22 at 22:55

1 Answers1

0

if you check github.com/aws/aws-sdk-go-v2/feature. available packages are dynamodb/attributevalue or dynamodb/expression. Looks like you are using attribute value so the import should have "github.com/aws/aws-sdk-go-v2/feature/dynamodb/attributevalue"

Manoj Guglani
  • 134
  • 1
  • 11
  • I try to import that but when I save my main.go file the import is removed. Do you know how I can stop this? – echough1 Aug 16 '22 at 13:28
  • yeah looks like that's not the right import. If you check the dyanmodb API documentation. the right path package to import is github.com/aws/aws-sdk-go-v2/feature/dynamodb/types. See the code in go playground https://go.dev/play/p/giSzGbodA97 – Manoj Guglani Aug 18 '22 at 03:16