go - 软件包handlers/gql.go不在GOROOT中(/usr/local/go/src/handlers/gql.go)

标签 go go-modules

我正在尝试导入本地软件包,之前在其他项目中都已经这样做。我不确定如果我做错了什么,或者我在环境中摔坏了,我尝试去1.14 && 1.15。

go mod init github.com/malikiah/go-backend
go run main.go
返回错误:
handlers/gql.go: package handlers/gql.go is not in GOROOT (/usr/local/go/src/handlers/gql.go)
我不确定自己在做什么错。我只希望能够导入本地包。
main.go
package main

import (
    "context"
    "flag"

    //"io"
    "log"
    "net/http"
    "os"
    "os/signal"
    "time"

    //Handlers
    "handlers/gql.go"

    //Middleware
    
    //Services

    //External Packages
    "github.com/gorilla/mux"


)

func main() {

    var wait time.Duration
    flag.DurationVar(&wait, "graceful-timeout", time.Second*15, "the duration for which the server gracefully wait for existing connections to finish - e.g. 15s or 1m")
    flag.Parse()

    port := "3000"
    // Init Router
    router := mux.NewRouter()

    // // route handlers / endpoints
    // graphiqlHandler, err := gql.NewGraphiqlHandler("/api")
    // if err != nil {
    //     panic(err)
    // }

    // router.HandleFunc("/graphql", gqlHandler())
    // router.HandleFunc("/graphiql", graphiqlHandler)
    // router.HandleFunc("/api", errorhandlers.HealthCheckHandler).Methods("GET").Name("HealthCheck")
    // router.HandleFunc("/login", userhandlers.LoginHandler).Methods("POST").Name("Login")
    // router.HandleFunc("/register", userhandlers.RegistrationHandler).Methods("POST").Name("Register")

    // router.NotFoundHandler = http.HandlerFunc(errorhandlers.NotFoundHandler)

    // router.Use(middleware.LoggingMiddleware)

    log.Println("Gopher army ready and is listening on TCP port " + port + "...")
    // Custom server
    srv := &http.Server{
        Addr: "127.0.0.1:" + port,
        // Prevents Slowloris Attacks
        WriteTimeout: time.Second * 15,
        ReadTimeout:  time.Second * 15,
        IdleTimeout:  time.Second * 60,
        Handler:      router, //Passing gorilla mux instance
    }

    go func() {
        if err := srv.ListenAndServe(); err != nil {
            log.Println(err)
        }
    }()

    c := make(chan os.Signal, 1)
    // Acceptes SIGINT for graceful shutdown.

    signal.Notify(c, os.Interrupt)

    // Block until it receives signal.
    <-c

    // Create a deadline to wait for.
    ctx, cancel := context.WithTimeout(context.Background(), wait)
    defer cancel()
    // Doesn't block if no connections, but will otherwise wait until the timeout deadline.
    srv.Shutdown(ctx)

    log.Println("shutting down...")
    os.Exit(0)

}

最佳答案

导入go软件包时,将导入文件夹而不是直接文件。

关于go - 软件包handlers/gql.go不在GOROOT中(/usr/local/go/src/handlers/gql.go),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63440978/

相关文章:

opencv - 垃圾收集不适用于 []gocv.Mat

go - "No filesystem found for scheme gs"在谷歌云平台运行数据流时

go - 有没有更简单的方法来更新本地 Go 包

git - 如何在另一个私有(private) Go 项目(作为模块)中导入私有(private) Go 库(作为模块)

c++ - golangatomic.Load 有获取语义吗?

go - 拨一个 jsonrpc 连接到比特币

go - 在 Bitbucket 管道中交叉编译 Golang

go - 如何使用 go modules 删除已安装的包

go - 人们无法使用 gomodules 使用我的 v2+ 语义版本控制版本

go - 如何使用 Go Modules 识别依赖链