Home > Software engineering >  Golang Broken Import
Golang Broken Import

Time:10-05

I got an error when trying import this package:

"github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-grpc-gateway"
"github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-openapiv2"
"google.golang.org/grpc/cmd/protoc-gen-go-grpc"
"google.golang.org/protobuf/cmd/protoc-gen-go"

the errors show:

could not import github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-grpc-gateway (no required module provides package "github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-grpc-gateway")

the errors are the same no required module provides package

here's my go-env:

GO111MODULE="on"

GOARCH="amd64"

GOBIN="/usr/local/go/bin"

GOCACHE="/home/servumtopia/.cache/go-build"

GOENV="/home/servumtopia/.config/go/env"

GOEXE=""

GOEXPERIMENT=""

GOFLAGS

GOHOSTARCH="amd64"

GOHOSTOS="linux"

GOINSECURE=""

GOMODCACHE="/home/servumtopia/go/pkg/mod"

GONOPROXY=""

GONOSUMDB=""

GOOS="linux"

GOPATH="/home/servumtopia/go"

GOPRIVATE=""

GOPROXY="https://proxy.golang.org,direct"

GOROOT="/usr/local/go"

GOSUMDB="sum.golang.org"

GOTMPDIR=""

GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"

GOVCS=""

GOVERSION="go1.19"

GCCGO="gccgo"

GOAMD64="v1"

AR="ar"

CC="gcc"

CXX="g  "

CGO_ENABLED="1"

GOMOD="/home/servumtopia/CODE/GO/sqlc/go.mod"

GOWORK=""

CGO_CFLAGS="-g -O2"

CGO_CPPFLAGS=""

CGO_CXXFLAGS="-g -O2"

CGO_FFLAGS="-g -O2"

CGO_LDFLAGS="-g -O2"

PKG_CONFIG="pkg-config"

GOGCCFLAGS="-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build2576989244=/tmp/go-build -gno-record-gcc-switches"

the code:

//go:build tools
//  build tools

package tools

import (
    _ "github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-grpc-gateway"
    _ "github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-openapiv2"
    _ "google.golang.org/grpc/cmd/protoc-gen-go-grpc"
    _ "google.golang.org/protobuf/cmd/protoc-gen-go"
)

file structure:

sqlc
    |.github
    |api
    |db
    |gapi
    |pb
    |proto
    |token
    |tools---tools.go (where i put the file)
    |util
    app.env
    go.mod
    go.sum
    main.go
    makefile
    sqlc.yaml 

Explanation:

the go.mod file were adding the imported package

github.com/grpc-ecosystem/grpc-gateway/v2 v2.11.3
google.golang.org/grpc/cmd/protoc-gen-go-grpc v1.2.0
google.golang.org/protobuf v1.28.1

i try to import the package in the main.go file and i got this error when i run the code.

main.go:23:2: import "github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-grpc-gateway" is a program, not an importable package
main.go:24:2: import "github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-openapiv2" is a program, not an importable package
main.go:25:2: import "google.golang.org/grpc/cmd/protoc-gen-go-grpc" is a program, not an importable package
main.go:26:2: import "google.golang.org/protobuf/cmd/protoc-gen-go" is a program, not an importable package

CodePudding user response:

Those packages you're trying to import are a main package. You cannot import a main package as they are supposed to be compiled into programs (and you need a main package of your own).

If you check the source code, you'll see that this package has only a main.go file, which there's nothing that you can even use inside (it only has two non-exported functions).

The same is true for the other packages in question.

The best I could find was an official documentation on how to create a main.go file: https://grpc-ecosystem.github.io/grpc-gateway/docs/tutorials/creating_main.go/ -- I haven't read it thoroughly, tough.

CodePudding user response:

Did you initialize go module? If you haven't try these steps.

1. Create a module

go mod init github.com/youraccount/yourpackge

2. Create main.go

You already have your code here

2. Run go mod tidy from project root

go mod tidy

This should fix your imports.

You can individually go get them as well. Run these commands from your project root (where your go.mod is).

go get google.golang.org/protobuf/cmd/protoc-gen-go
go get github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-grpc-gateway
go get google.golang.org/grpc/cmd/protoc-gen-go-grpc
go get github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-openapiv2
  • Related