doushan1850 2019-07-24 15:00
浏览 67
已采纳

尝试使用本地子模块时出现问题

I'm trying to use go modules with some local code that hasn't been pushed to github yet (golang version is 1.12.7)

So far I have 3 modules that are all under the same parent root (sibiling folders). maps and go-database-util are already pushed, but the module should be using whatever is in my local (and I haven't yet created a git repo for go-log-util), so I don't think this is relevant.

/maps
    go.mod
    go.sum
    main.go
    /api
        ...more files (just a regular package, not a module)

/go-database-util
    db.go
    go.mod
    go.sum

/go-log-util
    log.go
    go.mod
    go.sum

Both go-database-util and go-log-util are libraries, no main package is provided. Interestingly, go-database-util works fine when imported into /maps/main.go, but that's not the case when attempting to import go-log-util. This is what I get when trying to go build maps:

build github.com/X/maps: cannot load github/X/go-log-util: cannot find module providing package github/X/go-log-util

What is going on here? I have been struggling with moving from go dep to go modules but no luck so far.

The relevant code is as follows.

Many thanks! :)

/maps/main.go

package main

import (
    "fmt"
    "net/http"
    "os"
    "os/signal"
    "syscall"
    "time"

    log "github/X/go-log-util"

    dbUtil "github.com/X/go-database-util"
    "github.com/X/maps/api"
    "github.com/gorilla/mux"
    "github.com/jinzhu/gorm"
    "github.com/namsral/flag"
    "github.com/sirupsen/logrus"
    "github.com/urfave/negroni"
)

type configuration struct {
    Addr       string
    LogLevel   string
    DBSettings dbUtil.Settings
}

var (
    config configuration
    db     *gorm.DB
)

func main() {
    loadConfig()
    flag.Parse()
    logrus.Infof("Using config: %#v", config)
    var err error
    db, err = dbUtil.Setup(config.DBSettings)
    if err != nil {
        logrus.Fatal(err)
    }

    n := negroni.New()
    n.Use(negroni.HandlerFunc(log.LoggingMiddleware))
    router := mux.NewRouter()
    userRoutes := mux.NewRouter()
    userRoutes.HandleFunc("/api/maps/info", api.HandleRead).Methods("GET")
    n.UseHandler(router)
    srv := &http.Server{
        Handler: n,
        Addr:    config.Addr,
    }

    setupCleanupHandler()
    logrus.Fatal(srv.ListenAndServe())
}

func loadConfig() {
    flag.StringVar(&config.Addr, "addr", "localhost:8080", "HTTP service address")
    flag.StringVar(&config.LogLevel, "log-level", "debug",
        "Application logging level (debug, info, warning, error, fatal, panic)")

    var dbPassword string
    var connMaxLifetime int
    flag.StringVar(&config.DBSettings.Host, "db-host", "", "DB host")
    flag.StringVar(&config.DBSettings.User, "maps-db-username", "", "DB user")
    flag.StringVar(&config.DBSettings.DBName, "maps-db-name", "X", "DB name")
    flag.StringVar(&dbPassword, "maps-db-password", "", "DB password")
    flag.IntVar(&config.DBSettings.MaxIdleConns, "db-max-idle-conns", 10, "DB max idle connections")
    flag.IntVar(&config.DBSettings.MaxOpenConns, "db-max-open-conns", 100, "DB max open connections")
    flag.BoolVar(&config.DBSettings.LogEnabled, "db-enable-log", false, "Enable detailed gorm log")
    flag.IntVar(&connMaxLifetime, "db-max-lifetime-conns", 5, "DB max connection lifetime (minutes)")
    config.DBSettings.Password = &dbPassword
    config.DBSettings.ConnMaxLifetime = time.Duration(connMaxLifetime) * time.Minute
}

func healthHandler(w http.ResponseWriter, r *http.Request) {
    fmt.Fprint(w, "OK SO")
}

func setupCleanupHandler() {
    sigs := make(chan os.Signal, 2)
    signal.Notify(sigs, os.Interrupt, syscall.SIGTERM)
    go func() {
        <-sigs
        logrus.Info("Server shutdown, cleaning up resources")
        db.Close()
        os.Exit(1)
    }()
}

/maps/go.mod

module github.com/X/maps

go 1.12

require (
    github.com/X/go-database-util v0.0.0
    github.com/X/go-log-util v0.0.0
    github.com/gorilla/mux v1.6.2
    github.com/jinzhu/gorm v1.9.10
    github.com/namsral/flag v1.7.4-pre
    github.com/sirupsen/logrus v1.4.2
    github.com/urfave/negroni v1.0.0
)

replace github.com/X/go-log-util => ../go-log-util

replace github.com/X/go-database-util => ../go-database-util

/go-database-util/db.mod

// Package db provides utility functions to perform database related tasks.
// While most of the code is generic, it assumes to be working using a postgres driver.
package db

import (
    "fmt"
    "time"

    "github.com/jinzhu/gorm"
    // required in order to load postgres driver.
    _ "github.com/jinzhu/gorm/dialects/postgres"
    "github.com/sirupsen/logrus"
)

// Settings store a database main configuration options.
type Settings struct {
    Host     string
    User     string
    DBName   string
    Password *string

    MaxIdleConns    int
    MaxOpenConns    int
    ConnMaxLifetime time.Duration

    LogEnabled bool
}

// Setup configures the settings for database connections.
func Setup(s Settings) (*gorm.DB, error) {
    connURL := fmt.Sprintf("host=%s user=%s dbname=%s sslmode=disable password=%s",
        s.Host, s.User, s.DBName, *s.Password)
    db, err := gorm.Open("postgres", connURL)
    if err != nil {
        return nil, err
    }

    db.DB().SetMaxIdleConns(s.MaxIdleConns)
    db.DB().SetMaxOpenConns(s.MaxOpenConns)

    // broken connections may not be detected; would need a dedicate goroutine
    // setting this to just a few seconds / minutes will solve a lot of issues
    // see https://github.com/go-sql-driver/mysql/issues/529
    db.DB().SetConnMaxLifetime(s.ConnMaxLifetime)
    db.LogMode(s.LogEnabled)
    logger := logrus.New()
    db.SetLogger(logger)
    return db, nil
}

go-database-util/go.mod

module github.com/X/go-database-util

go 1.12

require (
    github.com/jinzhu/gorm v1.9.10
    github.com/sirupsen/logrus v1.4.2
)

go-log-util/log.go

package log

import (
    "context"
    "net/http"
    "os"

    "github.com/sirupsen/logrus"
)

const (
    // RequestIDHeader is the header name for the request id.
    RequestIDHeader = "X-Request-Id"
    // UserIDKey is the key for the user id inside the logger fields.
    UserIDKey = "UserID"
)

// RequestContext returns a new context with the argument context fields
// and a request id.
func RequestContext(ctx context.Context, req *http.Request) context.Context {
    requestID := req.Header.Get(RequestIDHeader)
    reqContext := context.WithValue(ctx, RequestIDHeader, requestID)
    return reqContext
}

// L returns logrus entry with context information.
func L(ctx context.Context) *logrus.Entry {
    logger := logrus.New()
    Configure(logger)

    l := logrus.Fields{
        RequestIDHeader: RequestID(ctx),
    }

    if id := UserID(ctx); id != 0 {
        l[UserIDKey] = id
    }

    return logger.WithFields(l)
}

// RequestID returns the request id from the context.
func RequestID(ctx context.Context) string {
    if id, ok := ctx.Value(RequestIDHeader).(string); ok {
        return id
    }
    return ""
}

// UserID returns the user id from the context.
func UserID(ctx context.Context) uint64 {
    if id, ok := ctx.Value(UserIDKey).(uint64); ok {
        return id
    }
    return 0
}

// Setup initializes and sets configuration options for the logger.
func Setup(logLevel string, logger *logrus.Logger) {

    logger.Formatter = &logrus.TextFormatter{
        FullTimestamp: true,
    }

    logger.Out = os.Stdout
    level, _ := logrus.ParseLevel(logLevel)
    logger.Level = level
}

// Configure initializes and set configuration options for the logger.
func Configure(logger *logrus.Logger) {

    logger.Formatter = &logrus.TextFormatter{
        FullTimestamp: true,
    }

    logger.Out = os.Stdout
    logger.Level = logrus.StandardLogger().Level
}

// LoggingMiddleware creates a middleware that logs useful information from an
// *http.Request
func LoggingMiddleware(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
    ctx := RequestContext(r.Context(), r)
    logFields := logrus.Fields{
        "remoteAddr":      r.RemoteAddr,
        "method":          r.Method,
        "url":             r.URL,
        "x-forwarded-for": r.Header.Get("X-Forwarded-For"),
    }

    L(ctx).WithFields(logFields).Info("Incoming request")
    next(w, r.WithContext(ctx))
}

go-log-util/go.mod

module github.com/X/go-log-util

go 1.12

require github.com/sirupsen/logrus v1.4.2
  • 写回答

1条回答 默认 最新

  • drny20290570 2019-07-25 12:32
    关注

    Silly me, there was a typo in the import path at /maps/main.go: "github/X/go-log-util". .com was missing! :)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题