1
votes

Im trying to redeploy a golang function that was working in Google Cloud Functions with Go 1.11 but now that I have to deploy in Go 1.13 I'm getting the following error:

Build failed: the module path in the function's go.mod must contain a dot in the first path element before a slash, e.g. example.com/module, found: iotpubsub; Error ID: 90dcdd2e

Function.go:

package iotpubsub

import (
    "context"
    "errors"
    "fmt"
    "log"

    iot "cloud.google.com/go/iot/apiv1"
    iotpb "google.golang.org/genproto/googleapis/cloud/iot/v1"
)

// Configuration options
// You must update these values to match your environment!
const (
    region     = "us-central1"
    projectID  = "myprojectID"
    registryID = "myRegistryID"
)

var client *iot.DeviceManagerClient

func init() {
    var err error
    client, err = iot.NewDeviceManagerClient(context.Background())
    if err != nil {
        log.Fatalf("iot.NewDeviceManagerClient: %s", err)
    }
}

// PubSubMessage implements the Pub/Sub model.
type PubSubMessage struct {
    Data       []byte            `json:"data"`
    Attributes map[string]string `json:"attributes"`
}

// Send sends the Pub/Sub message to the device.
func Send(ctx context.Context, m PubSubMessage) error {
    deviceID, ok := m.Attributes["deviceId"]
    if !ok {
        return errors.New("deviceId is missing in Attributes")
    }
    subFolder, ok := m.Attributes["subFolder"]
    if !ok {
        return errors.New("subFolder is missing in Attributes")
    }

    deviceName := fmt.Sprintf("projects/%s/locations/%s/registries/%s/devices/%s", projectID, region, registryID, deviceID)

    _, err := client.SendCommandToDevice(ctx, &iotpb.SendCommandToDeviceRequest{
        Name:       deviceName,
        BinaryData: m.Data,
        Subfolder:  subFolder,
    })
    if err != nil {
        return fmt.Errorf("SendCommandToDevice: %s", err)
    }

    return nil
}

go.mod:

module iotpubsub

go 1.13

require (
    cloud.google.com/go v0.39.0
    google.golang.org/genproto v0.0.0-20190605220351-eb0b1bdb6ae6
)

What should I do to fix the error? Thanks in advance.

1
The error message tells you exactly what's wrong, and therefore how to fix it. Go module names are of the form "domain.tld/path/to/module". iotpubsub is not a valid module name. - Adrian

1 Answers

2
votes

Try to call your module iotpubsub.com or anything else that looks like a proper domain. Official documentation is here: https://golang.org/ref/mod#go-mod-file-ident

Also check out https://blog.golang.org/migrating-to-go-modules and the other posts in the series.