Golang Broken Import - go

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

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.

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

linker command failing for particular golang test involving "net/http" and "time" packages

I'm new to web development and golang. I'm trying to learn go from here. Without any issues I was able to learn till concurrency part, however, I'm facing issues while trying to learn select statements. Here is what I did. hello.go contains the function, while hello_test.go has the testing code. This is the hello.go code.
package lol
func Racer(a, b string) (winner string) {
return
}
Below is the testing code
package lol
import "testing"
func TestRacer(t *testing.T) {
slowURL := "http://www.facebook.com"
fastURL := "http://www.quii.co.uk"
want := fastURL
got := Racer(slowURL, fastURL)
if got != want {
t.Errorf("got %q, want %q", got, want)
}
}
The test is failing just as expected with message hello_test.go:13: got "", want "http://www.quii.co.uk". Now I'm trying to write some more code to hello.go to make it pass.
package lol
import(
"time"
"net/http"
)
func Racer(a, b string) (winner string) {
startA := time.Now()
http.Get(a)
aDuration := time.Since(startA)
startB := time.Now()
http.Get(b)
bDuration := time.Since(startB)
if aDuration < bDuration {
return a
}
return b
}
When I'm testing now, it is giving me # runtime/cgo ld: unsupported tapi file type '!tapi-tbd' in YAML file '/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/lib/libSystem.tbd' for architecture x86_64 clang-10: error: linker command failed with exit code 1 (use -v to see invocation) FAIL module [build failed]. After searching on internet forums on this error, I did sudo rm -rf /Library/Developer/CommandLineTools and reinstalled them back, but didn't help. I think it may be due to the modules I'm trying to import but I'm not sure. I don't know if it is relevant or not but I updated to macOS Big Sur two days before facing the issue. Please help in resolving the issue. TIA
Edit: go env is returning
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/{user_name}/Library/Caches/go-build"
GOENV="/Users/{user_name}/Library/Application Support/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/{user_name}/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/{user_name}/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/Cellar/go/1.16.5/libexec"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/Cellar/go/1.16.5/libexec/pkg/tool/darwin_amd64"
GOVCS=""
GOVERSION="go1.16.5"
GCCGO="gccgo"
AR="/Users/{user_name}/opt/anaconda3/bin/x86_64-apple-darwin13.4.0-ar"
CC="x86_64-apple-darwin13.4.0-clang"
CXX="x86_64-apple-darwin13.4.0-clang++"
CGO_ENABLED="1"
GOMOD="/dev/null"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -arch x86_64 -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/zc/4r25vdyj4cv3ynjx64633ly40000gn/T/go-build3491450963=/tmp/go-build -gno-record-gcc-switches -fno-common"
Edit 2:
The test started working when I ran CGO_ENABLED=0 go test -v ./.... It returned the expected output.
=== RUN TestRacer
hello_test.go:13: got "http://www.facebook.com", want "http://www.quii.co.uk"
--- FAIL: TestRacer (2.52s)
FAIL
FAIL module 3.076s
FAIL
I'm still trying to understand why I need to add CGO_ENABLED=0 to for this particular test.
In my case it was R Studio installed through Anaconda package manager. After it was removed, the linker started working as expected.

Cannot find package within project directory

I've been trying to solve this problem for the past two hours. I cannot, for the life of me, understand how the folks who developed Go did such a fine job with the language but such a terrible job with package management.
Here is my go env output:
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/<me>/Library/Caches/go-build"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/<me>/Documents/Proj/go"
GORACE=""
GOROOT="/usr/local/opt/go/libexec"
GOTMPDIR=""
GOTOOLDIR="/usr/local/opt/go/libexec/pkg/tool/darwin_amd64"
GCCGO="gccgo"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
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 -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/z4/b3lxy_51405_b8pb_680l4xh0000gn/T/go-build063693521=/tmp/go-build -gno-record-gcc-switches -fno-common"
Notice that my GOPATH and GOROOT are set.
Here is the structure of my go workspace (/Users/<me>/Documents/Proj/go):
go/
|-bin/
|-pkg/
|-src/
|--github.com/
|--user/
|--myproject/
|--client/
|--client.go
|--main.go
Here is my client.go file:
// client.go
package client
type MyClient struct {}
this is my main.go file:
// main.go
package main
import "client"
func main() {}
When I run go build in the project root, I get this error:
main.go:4:2: cannot find package "client" in any of:
/usr/local/opt/go/libexec/src/transport (from $GOROOT)
/Users/<me>/Documents/Proj/go/src/transport (from $GOPATH)
The first time this happened, I tried to correct the path above:
// main.go
package main
import "github.com/user/myproject/client"
func main() {}
Then I got this error:
main.go:4:2: cannot find package "client" in any of:
/usr/local/opt/go/libexec/src/transport (from $GOROOT)
/Users/<me>/Documents/Proj/go/src/github.com/user/myproject/transport (from $GOPATH)
Which is bizarre, since the package exists at that exact path.
What am I doing wrong??
You should not import client but which path is coming from GOPATH/src, in this particular case github.com/user/myproject/client because that is your path.
I see that the way you describe your problem is not exactly the way it is on your computer. You replaced some things. That is understandable, you do not want to tell the world how your computer looks like. So you replaced your name with "<me>".
You also replaced the projectname, with MyProject in your question, that is also, just like the user name, a potential spot for an error which we cannot see, because you replaced it. Never use long names in development-paths on Windows, because Windows is doing dirty tricks with them, sometimes.
I am sure there is something the matter which is a problem. Try using your short-directory name in the go-path. Check this: https://superuser.com/questions/1182040/strange-short-path-name-on-windows

Go package imports self, and can't find itself in it the GOPATH

Every once in awhile, I'll use a go package that has a go get like : go get github.com/<user>/aRepo and in its own import statement have import "github.com/<user>/aRepo". So it imports itself using a the "go gettable" path. Sometimes this works fine; however, sometimes I get
package github.com/<user>/aRepo
imports github.com/<user>/aRepo
imports github.com/<user>/aRepo: cannot find package "github.com/<user>/aRepo" in any of:
/usr/local/go/src/github.com/<user>/aRepo (from $GOROOT)
/home/me/go/src/github.com/<user>/aRepo (from $GOPATH)
I set my GOPATH with:
export GOPATH=$GOPATH:/home/me/go:`godep path`
It's very unclear as to why this would happen, any ideas as to why?
EDIT
my go env:
GOARCH="amd64"
GOBIN=""
GOCHAR="6"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH=":/home/vagrant/go"
GORACE=""
GOROOT="/usr/local/go"
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
CC="gcc"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0"
CXX="g++"
CGO_ENABLED="1"
So it turns out it was a permissions issue since I was using a vagrant box with a synced folder, vagrant set the entire path that synced folder created as 744 root:root. Therefore, I could not write to that folder as a non-root user, so that error message is actually coming from godep trying to install itself and not finding its src.

go install not working

I am using go version go1.5.1 linux/amd64 on debian 8.2 3.16.0-4-amd64. I installed golang using https://golang.org/doc/install.
This is what i put in my ~/.profile file
export GOPATH=$HOME/go
export PATH=$PATH:/usr/local/go/bin:/home/shivams/go/bin
Running go env on my machine is giving this output
GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/shivams/go"
GORACE=""
GOROOT="/usr/local/go"
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GO15VENDOREXPERIMENT=""
CC="gcc"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0"
CXX="g++"
CGO_ENABLED="1"
Directory structure inside /home/shivams/go is {pkg,src,bin}. Inside src directory it is like test/hello.go.
If i am in src dir and run go run test/hello.go it is running perfectly. Also if i run go build test/hello.go it will create one executable hello file in same directory.
But if i try to run go install test/hello.go then i get this error
go install: no install location for .go files listed on command line (GOBIN not set). If i set GOBIN explicitly then i am not able to see this error.
From what i read my understanding is that if GOPATH is set there is no need to set GOBIN variable.
Am i missing anything here? This is the first time i am trying go and not able to get this working.
As #JimB states, install is a command designed for packages. Just to give you a clear example, here is what I get when I run the go env command.
GOARCH="amd64"
GOBIN="/Users/quazinafiulislam/Code/go/ogolang/bin"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/quazinafiulislam/Code/go/ogolang"
GORACE=""
GOROOT="/usr/local/Cellar/go/1.5.1/libexec"
GOTOOLDIR="/usr/local/Cellar/go/1.5.1/libexec/pkg/tool/darwin_amd64"
GO15VENDOREXPERIMENT=""
CC="clang"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fno-common"
CXX="clang++"
CGO_ENABLED="1"
You will see that the GOPATH is set to my project root, ~/Code/go/ogolang. Now, lets see whats inside my project root.
.
├── bin
├── pkg
│   └── darwin_amd64
└── src
├── bitbucket.org
├── github.com
├── golang.org
├── words
└── wordtest
As you can see I have a couple of packages. One of them is wordtest. I can use a go install on the words or wordtest packages. So, lets run go install words and see what happens to the directory.
.
├── bin
│   └── words
├── pkg
│   └── darwin_amd64
└── src
├── bitbucket.org
├── github.com
├── golang.org
├── words
└── wordtest
Now, we can see that we have a words binary created for us(inside the bin directory), and we can run it by invoking ./bin/words.
According to Games Brainiac go install work on packages is correct. But let me add some details into it regarding creating packages using go install in main directory.
.
├── bin
│ └── app
├── pkg
│ └── linux_amd64
| └── user
| └── handlers.a
└── src
├── bitbucket.org
├── github.com
└── user
└── app
├── main.go
└── handlers
└──handlers.go
Have a look at above directory structure, I have created a package in a folder handlers having source file handlers.go .And I wants to build that package, I will run go build inside handlers folder which just show our file has no compilation errors and returns nothing in that case.
To build a package with main file we need to run go install inside app folder. This will create handlers.a package object inside $GOPATH/pkg and executable file inside $GOPATH/bin with name of folder (app in this case) in which we have main.go file using package main.

Go 'Can't Find Import'

I'm attempting to incorporate Heroku's log-shuttle library (https://github.com/heroku/log-shuttle) into a Go project I'm working on. The tool is primarily designed to be run as an independent binary, but I'm hoping to integrate it in a different way in my tool.
So I get the library:
$ go get github.com/heroku/log-shuttle
$ ls $GOPATH/src/github.com/heroku/log-shuttle/
batcher.go Godeps ...
...
which returns successfully. Then I try to import the library:
package myPackage
import (
"github.com/heroku/log-shuttle"
"fmt"
"log"
)
...
Great. But now I go to go build and...
$ go build
# github.com/<my project>
logWriter/log_shuttle_writer.go:5: can't find import: "github.com/heroku/log-shuttle"
I have my GOPATH set correctly (I believe):
$ go env
GOARCH="amd64"
GOBIN=""
GOCHAR="6"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/jeff/go/"
GORACE=""
GOROOT="/usr/lib/go"
GOTOOLDIR="/usr/lib/go/pkg/tool/linux_amd64"
TERM="dumb"
CC="gcc"
GOGCCFLAGS="-g -O2 -fPIC -m64 -pthread"
CXX="g++"
CGO_ENABLED="1"
And the package is installed and built, but it doesn't want to import. As you can see, there are non non-ASCII chars in the package path (though the hyphen is non-alpha-numeric), and I can't find any more info about what could be causing this problem.
Not sure how it would matter, but I am using godep to try to manage my dependencies.
Thanks in advance.
github.com/heroku/log-shuttle is a main package, not an importable library, meaning it's meant to be compiled and run as a binary.

Resources