Golang: 生成核心失败:无法加载 github.com。

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

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

问题

我安装了golang,并从这个教程开始:https://www.howtographql.com/graphql-go/1-getting-started/

当我运行:

go run github.com/99designs/gqlgen generate 

我得到了以下错误:

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

我的设置有什么问题?这是我的gopath:

/Users/my-pc-user/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

答案1

得分: 0

TLDR

$ cd 你的项目路径/
$ print '// +build tools\npackage tools\nimport _ "github.com/99designs/gqlgen"' | gofmt > ./tools.go
$ echo 'package model' | gofmt > ./graph/model/doc.go
$ go get .

Explanation

根据快速入门指南,你应该创建一个包含已经被服务器导入的生成代码的包:

package main

import (
	"log"
	"net/http"
	"os"

	"github.com/99designs/gqlgen/graphql/handler"
	"github.com/99designs/gqlgen/graphql/playground"
	"你的模块名/graph"
	"你的模块名/graph/generated"
)

由于你的模块名/graph/generated没有*.go文件,你无法启动服务器,如果尝试启动,会出现以下错误:

graph/schema.resolvers.go:10:2: no required module provides package 你的模块名/graph/generated; to add it:

要生成该包,你需要执行go run github.com/99designs/gqlgen generate,但是还有另一个问题:gqlgen生成的代码使用了另一个尚不存在的包,即你的模块名/graph/model

为了在生成过程中不丢失间接依赖项,需要额外添加build约束。这就是第一步使用下划线导入的原因。

如果你将任何带有package指令的*.go文件放入该目录中,现在应该一切正常:

$ cd 你的项目路径/
$ print '// +build tools\npackage tools\nimport _ "github.com/99designs/gqlgen"' | gofmt > ./tools.go
$ echo 'package model' | gofmt > ./graph/model/doc.go
$ go get .
英文:

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 .

huangapple
  • 本文由 发表于 2022年2月21日 03:04:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/71197796.html
匿名

发表评论

匿名网友

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

确定