Golang: generating core failed: unable to load github.com - go

I installed golang and started with this tutorial: https://www.howtographql.com/graphql-go/1-getting-started/
When I run:
go run github.com/99designs/gqlgen generate
I get:
reloading module info
generating core failed: unable to load github.com/my-user/hackernews/graph/model - make sure you're using an import path to a package that exists
exit status 1
What is wrong with my setup?
This is my gopath
/Users/my-pc-user/go

TLDR
$ cd your_project_path/
$ print '// +build tools\npackage tools\nimport _ "github.com/99designs/gqlgen"' | gofmt > ./tools.go
$ echo 'package model' | gofmt > ./graph/model/doc.go
$ go get .
Explanation
According to a quick start guide, you should create a package with generated code that actually is already imported by your server:
package main
import (
"log"
"net/http"
"os"
"github.com/99designs/gqlgen/graphql/handler"
"github.com/99designs/gqlgen/graphql/playground"
"your_module_name/graph"
"your_module_name/graph/generated"
)
Because of the fact that your_module_name/graph/generated has no *.go files you cannot start a server and if you try you will get an error like:
graph/schema.resolvers.go:10:2: no required module provides package your_module_name/graph/generated; to add it:
To generate that package you will need to execute go run github.com/99designs/gqlgen generate, but there is another problem: gqlgen generates code that uses another package that is still doesn't exist yet, i.e. your_module_name/graph/model.
Additional step adding build constraints is required to not drop indirect dependency while generation process. That's why there is the first step with underscore import.
And if you put any *.go file to that directory with package directive — everything should works now:
$ cd your_project_path/
$ print '// +build tools\npackage tools\nimport _ "github.com/99designs/gqlgen"' | gofmt > ./tools.go
$ echo 'package model' | gofmt > ./graph/model/doc.go
$ go get .

Related

Unable to debug go in vscode

I'm not able to debug a go file in vscode. I want to debug mrcorordinator.go in package main but the main package also contain other files.Here is the main package structure.
main
diskvd.go
lockc.go
lockd.go
mrcoordinator.go
mrworker.go
pbc.go
pbd.go
while debugging mrcoordinator.go, I'm getting Error like this
Build Error: go build -o /home/sanketbondre/myspace/6.824/src/main/__debug_bin -gcflags all=-N -l .
diskvd.go:19:8: no required module provides package 6.824/diskv; to add it:
go get 6.824/diskv
lockc.go:7:8: no required module provides package 6.824/lockservice; to add it:
go get 6.824/lockservice
pbc.go:21:8: no required module provides package 6.824/pbservice; to add it:
go get 6.824/pbservice
viewd.go:8:8: no required module provides package 6.824/viewservice; to add it:
go get 6.824/viewservice (exit status 1)
Does anyone know how debug specific go file in vscode(here I want to debug mrcoordinator.go only).

VSCode import problem with windows API call

When importing golang.org/x/sys/windows in VSCode, I can only choose SIGDescribe, nothing else.
Hovering over the import, following errors appear.
error while importing golang.org/x/sys/windows: build constraints exclude all Go files in /home/username/go/pkg/mod/golang.org/x/sys#v0.0.0-20210630005230-0f9fa26af87c/windows
could not import golang.org/x/sys/windows (no required module provides package "golang.org/x/sys/windows")compilerBrokenImport
The manual command go get golang.org/x/sys/windows gives the following error message
Command 'gopls.go_get_package' failed: Error: err: exit status 1: stderr: package golang.org/x/sys/windows: build constraints exclude all Go files in /home/username/go/pkg/mod/golang.org/x/sys#v0.0.0-20210630005230-0f9fa26af87c/windows .
I already re-installed Golang and updated GoTools in VSCode, no changes.
Goal: The following code below should work.
package main
import "golang.org/x/sys/windows"
func main() {
user32DLL := windows.NewLazyDLL("user32.dll")
}
OS: Ubuntu 21.04
GO Version: 1.16.6
Editor: VSCode 1.58.1
Make a folder somewhere something. Then make a file something/main.go:
package main
import "golang.org/x/sys/windows"
func main() {
println(windows.EVENTLOG_ERROR_TYPE == 1)
}
Then build:
go mod init something
go mod tidy
go build

Go import error: invalid version unknow revision

Code, file structure on left and error
package main
import (
"fmt"
"github.com/cilium/cilium/api/v1/models"
"github.com/cilium/cilium/pkg/client"
log "github.com/sirupsen/logrus"
)
func main() {
fmt.Println("Starting Application...")
fmt.Println("Terminating Application...")
}
This is the error I get when I run: go run main.go
github.com/cilium/cilium#1.7.2 requires github.com/optiopay/kafka#v0.0.0-2080809090225-01ce283b732b: ivalid version: unknown revision 01ce283b732b
I am using go mod file for my dependencies and my go version is latest 1.14
I also checked my file structure and I already have cilium#v1.7.2 under pkg/mod/github.com/cilium
I tried adding github.com/optiopay/kafka under my import statement in my code but i get the same exact error still.
I have no idea how to fix this, I was googling but I could fin no definite or clear answer which worked. Any help appreicated.
You are seeing this error because replace directives in go.mod are ignored by go mod/go get and the go.mod file of Cilium v1.7.2 contains many replace directives.
To workaround this issue, replicate the replace directives of the version of Cilium that you are trying to import into your own go.mod file. An example for Cilium v1.7 can be found in this repository.
seems optiopay/kafka is your private repo. You can configure ssh with git. and set your url in git config to use ssh instead of https.
Steps:
configure ssh (if not already done)
update git config ->
git config --system url."git#github.com:".insteadOf "https://github.com/"
Error - the version that go is trying to pull for that specific module that imports it is either outdated or invalid.
Solution - For example in my case i was getting this error below:
go: downloading github.com/myk4040okothogodo/tutorial/gen/go/proto/books v0.0.0-00010101000000-000000000000
github.com/myk4040okothogodo/tutorial/books imports
github.com/myk4040okothogodo/tutorial/books/server imports
github.com/myk4040okothogodo/tutorial/gen/go/proto/books: github.com/myk4040okothogodo/tutorial/gen/go/proto/books#v0.0.0-00010101000000-000000000000: invalid version: unknown revision 000000000000
as you can see above my module "github.com/myk4040okothogodo/tutorial/books/server" imports another module "github.com/myk4040okothogodo/tutorial/gen/go/proto/books:" this import throws the error above, so i go to my go.mod file and make the following changes:
1 module github.com/myk4040okothogodo/tutorial/books/server
2
3 go 1.18
4
5 replace github.com/myk4040okothogodo/tutorial/db => ../../db
6
7 replace github.com/myk4040okothogodo/tutorial/gen/go/proto/books => ../../gen/go/proto/books
8
9 require (
10 github.com/arangodb/go-driver v1.3.2
11 github.com/myk4040okothogodo/tutorial/db v0.0.0-00010101000000-000000000000
12 github.com/myk4040okothogodo/tutorial/gen/go/proto/books latest
13 google.golang.org/grpc v1.47.0
14 )
check above in line 12 where i put "latest" instead of the version number i.e "v0.0.0...."
I save the file and then i run "go mod tidy"
The compiler then changes the "latest" designation to an up to date version,i.e it will look as below after running the mod tidy command.
1 module github.com/myk4040okothogodo/tutorial/books/server
2
3 go 1.18
4
5 replace github.com/myk4040okothogodo/tutorial/db => ../../db
6
7 replace github.com/myk4040okothogodo/tutorial/gen/go/proto/books => ../../gen/go/proto/books
8
9 require (
10 github.com/arangodb/go-driver v1.3.2
11 github.com/myk4040okothogodo/tutorial/db v0.0.0-00010101000000-000000000000
12 github.com/myk4040okothogodo/tutorial/gen/go/proto/books v0.0.0-20220601171028-60237b9c9583
13 google.golang.org/grpc v1.47.0
14 )
PS: check the package where i make the changes, dont change the wrong imports

How to get and build all dependencies of go package which has no main function?

I have a following project:
myrepo/ # root package, no main() function
|-common/ # common utilties package
|-cmd/ # CLI root
| |-myrepo-a/ # CLI application A
| | |-main.go # CLI application A main package/function,
| | # setting up feature A for CLI use
| |-myrepo-b/ # CLI application B
| |-main.go # CLI application B main package/function,
| # setting up feature B for CLI use
|-aspecific/ # package with generic implementation for feature A
|-bspecific/ # package with generic implementation for feature B
|-generate.go # file dispatching //go:generate instructions
How to get and build dependencies of such myrepo project?
go get -v ./... invocation yields errors:
runtime.main_main·f: relocation target main.main not defined
runtime.main_main·f: undefined: "main.main"
Can i avoid manually specifying each of subpackages for such invocation as in go get ./common/... ./cmd/... ./aspecific/... ./bspecific/..., as such hard-coding might result in desync between actual project state and packages getting built?

Stringer tool complains about wrong archive header

I am trying to use go generate/stringer (golang.org/x/tools/cmd/stringer) to generate String() methods on enums. I have problems, which I believe, are because of slightly different format of .a packages on different systems. I have this file:
package main
import (
"math/rand"
)
//go:generate stringer -type=Foo
type Foo int;
const (
FooPrime Foo = iota
FooBis
)
func main() {
//Just use rand anywhere, otherwise we get a compiler error
rand.Seed(1)
}
Now if I run go generate example.go on my machine everything is all right: foo_string.go is created. However, on a test machine I get:
stringer: checking package: example.go:4:2: could not import math/rand (reading export data: /usr/lib64/go/pkg/linux_amd64/math/rand.a: go archive is missing __.PKGDEF)
Now, after some digging in the code I think that I get this error, because on my machine rand.a has the following header:
!<arch>
__.PKGDEF 0 0 0 644 2051
`
while on test machine it has the following header:
!<arch>
__.PKGDEF/ 0 399 399 100644 2051
`
I think that the crucial difference is slash after PKGDEFF. gcimporter refuses to process .a file, if it doesn't have __.PKGDEF header.
To check this, I edited manually gcimporter/exportdata.go and changed one of the line from this:
if name != "__.PKGDEF"
to this:
if name != "__.PKGDEF" && name != "__.PKGDEF\"
After this change (and compiling and installing everything) I was able to run go generate on example.go.
My questions are: why do I get this problem and how do I get rid of it (other then manually editing external library)?
What I can see from the spec for openSUSE's packaging they are disabling reinstallation of the standard library at updates. __.PKGDEF is a Go specific informational section, and some linker OpenSUSE has used has simply produced incompatible output.
There's nothing you can do except install a healthy Go from the official source.

Resources