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

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

我的设置有什么问题? 这是我的路线

/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 .

说明

根据 a quick start guide,您应该使用生成的代码创建一个包,该代码实际上已经由您的服务器导入:

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

由于 your_module_name/graph/generated 没有 *.go 文件,您无法启动服务器,如果您尝试启动,您将收到如下错误:

graph/schema.resolvers.go:10:2: no required module provides package your_module_name/graph/generated; to add it:

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

需要添加 build 约束的额外步骤才能在生成过程中不删除 indirect 依赖项。所以才会有第一步with underscore import.

并且如果您使用 package 指令将任何 *.go 文件放入该目录 - 现在一切都应该有效:

$ 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 .