Search code examples
goprotocol-buffersgrpc

Unable to use the protobuf package


It appears that I cannot import this package: github.com/golang/protobuf/proto

When I try to build or use go get I get:

cannot load github.com/golang/protobuf/proto: module github.com/golang/protobuf@latest (v1.3.2) found, but does not contain package github.com/golang/protobuf/proto

It is a popular package, I am surprised it does not seem to be working. https://godoc.org/github.com/golang/protobuf/proto#Marshal

Has anybody encountered this?

Update:

I am simply trying to import this:

import (
    "bytes"
    "context"
    "encoding/json"
    "errors"
    "fmt"
    "github.com/golang/protobuf/proto"
)

GoLang does not resolve proto in the above path...

I try to install like this:

$ go get github.com/golang/protobuf/proto
go: finding github.com/golang/protobuf/proto latest
go get github.com/golang/protobuf/proto: module github.com/golang/protobuf@upgrade (v1.3.2) found, but does not contain package github.com/golang/protobuf/proto

Update2, not sure how the file helps but here it is:

package main

import (
    "bytes"
    "context"
    "encoding/json"
    "errors"
    "fmt"
    "github.com/golang/protobuf/proto"
    "go_poc/plugins/com_styx_proto"
    "io/ioutil"
    "net/http"
    "time"
)

func init() {
    fmt.Println("styxBotDetect plugin is loaded!")
}

func (r registrable) RegisterHandlers(f func(
    name string,
    handler func(
        context.Context,
        map[string]interface{},
        http.Handler) (http.Handler, error),
)) {
    f(pluginName, r.registerHandlers)
}

func (r registrable) registerHandlers(ctx context.Context, extra map[string]interface{}, handler http.Handler) (http.Handler, error) {
// skipping some lines here

styxRqBytes, err := proto.Marshal(styxRq)
        if err != nil {
            http.Error(w, err.Error(), http.StatusNotAcceptable)
            return
        }

// more code

Solution

  • It turns out there was something wrong with the module cache, that's why the go tool was not able to fetch / update dependencies.

    In such cases, clearing the module cache (might) help:

    go clean -modcache