【发布时间】:2020-08-17 03:34:24
【问题描述】:
我正在尝试导入本地包,我之前在我的其他项目中已经这样做过。我不确定如果我做错了什么,或者我在我尝试过的环境中破坏了某些东西,请转到 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)
}
【问题讨论】:
-
导入
github.com/malikiah/go-backend/handlers。阅读How to write Go code。 -
不,因为如果我把它放在 go.mod 中它只会给出错误。它们只是我为这个本地项目制作的包。 @blackgreen
-
@MuffinTop 当我这样做时,它实际上会尝试从我没有实际存储库的 github 中提取它。
-
除非我需要一个 github repo 才能工作,但我认为你没有
-
@MuffinTop ohhhhhh 我的天啊我是个白痴我以前犯过这个错误......
标签: go go-modules