Golang导入错误

huangapple go评论75阅读模式
英文:

Golang Broken 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"

错误信息显示:

无法导入 "github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-grpc-gateway" 包(没有所需的模块提供该包)

错误信息都是相同的 没有所需的模块提供该包

这是我的 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"

代码如下:

//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"
)

文件结构如下:

├── api
├── db
│   ├── migration
│   ├── mock
│   ├── queries
│   └── sqlc
├── gapi
├── pb
├── proto
│   └── google
│       └── api
├── token
├── tools
└── util

解释:

go.mod 文件中添加了导入的包

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

我尝试在 main.go 文件中导入该包,但在运行代码时出现了这个错误。

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
英文:

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:

├── api
├── db
│   ├── migration
│   ├── mock
│   ├── queries
│   └── sqlc
├── gapi
├── pb
├── proto
│   └── google
│       └── api
├── token
├── tools
└── util

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

答案1

得分: 2

你正在尝试导入的那些包是一个main包。你不能导入一个main包,因为它们应该被编译成程序(而且你需要自己的main包)。

如果你查看源代码,你会发现这个包只有一个main.go文件,里面没有你可以使用的内容(它只有两个非导出函数)。

对于其他相关的包也是如此。

我找到的最好的资料是官方文档关于如何创建一个main.go文件:https://grpc-ecosystem.github.io/grpc-gateway/docs/tutorials/creating_main.go/ -- 我还没有仔细阅读它。

英文:

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.

答案2

得分: -1

你是否初始化了go模块?如果没有,请尝试以下步骤。

  1. 创建一个模块
go mod init github.com/youraccount/yourpackge
  1. 创建main.go

你已经在这里编写了你的代码。

  1. 从项目根目录运行go mod tidy
go mod tidy

这应该修复你的导入问题。

你也可以单独使用go get命令获取它们。从项目根目录(即你的go.mod所在的位置)运行以下命令。

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
英文:

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

huangapple
  • 本文由 发表于 2022年10月4日 19:07:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/73946975.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定