dpoh61610 2016-02-06 17:09 采纳率: 100%
浏览 305
已采纳

如何从Golang的子目录中正确导入软件包?

I am pretty new to Golang and trying to make a simple REST api app work.

Initially, everything was all fine since I had all code in the same directory under the main package.

But, now I am at a stage where I need to start refactoring code into sub-directories and packages. Unfortunately, I have not been able to compile the app successfully.

My GOPATH is set to: ~/.workspace The current app is at: ~/.workspace/src/gitlab.com/myapp/api-auth

This is how my current code organization is:

enter image description here

Here is my main.go

package main

import (
    "net/http"
    "os"
    "strings"

    "github.com/gorilla/context"
    "github.com/justinas/alice"
    "gopkg.in/mgo.v2"

    "gitlab.com/myapp/api-auth/middlewares"
)

func main() {
    privateKey := []byte(strings.Replace(os.Getenv("JWT_KEY"), "\
", "
", -1))

    conn, err := mgo.Dial(os.Getenv("MONGO_CONN"))

    if err != nil {
        panic(err)
    }

    defer conn.Close()
    conn.SetMode(mgo.Monotonic, true)

    ctx := appContext{
        conn.DB(os.Getenv("MONGO_DB")),
        privateKey,
    }

    err = ctx.db.C("users").EnsureIndex(mgo.Index{
        Key:        []string{"username"},
        Unique:     true,
        Background: true,
        Sparse:     false,
    })

    if err != nil {
        panic(err)
    }

    commonHandlers := alice.New(LoggingHandler, context.ClearHandler, RecoveryHandler, AcceptHandler, ContentTypeHandler)

    router := NewRouter()
    router.Post("/users", commonHandlers.Append(BodyParserHandler(UserResource{})).ThenFunc(ctx.userCreationHandler))
    router.Post("/sessions", commonHandlers.Append(BodyParserHandler(UserResource{})).ThenFunc(ctx.sessionCreationHandler))

    http.ListenAndServe(":8080", router)
}

type appContext struct {
    db         *mgo.Database
    privateKey []byte
}

Here is one of the middleware accept.go (Rest of the middleware are constructed similarly)

package middlewares

import "net/http"

// AcceptHandler ensures proper accept headers in requests
func AcceptHandler(next http.Handler) http.Handler {
    fn := func(w http.ResponseWriter, r *http.Request) {
        if r.Header.Get("Accept") != "application/vnd.api+json" {
            writeError(w, errNotAcceptable)
            return
        }

        next.ServeHTTP(w, r)
    }

    return http.HandlerFunc(fn)
}

This is the error I get when I run go build from root of my app.

# gitlab.com/utiliti.es/api-auth
./main.go:11: imported and not used: "gitlab.com/myapp/api-auth/middlewares"
./main.go:42: undefined: LoggingHandler
./main.go:42: undefined: RecoveryHandler
./main.go:42: undefined: AcceptHandler
./main.go:42: undefined: ContentTypeHandler
./main.go:45: undefined: BodyParserHandler
./main.go:46: undefined: BodyParserHandler
  • 写回答

1条回答 默认 最新

  • duan198811 2016-02-06 21:06
    关注

    The Go Programming Language Specification

    Qualified identifiers

    A qualified identifier is an identifier qualified with a package name prefix. Both the package name and the identifier must not be blank.

    QualifiedIdent = PackageName "." identifier .
    

    A qualified identifier accesses an identifier in a different package, which must be imported. The identifier must be exported and declared in the package block of that package.

    math.Sin  // denotes the Sin function in package math
    

    Import declarations

    An import declaration states that the source file containing the declaration depends on functionality of the imported package (§Program initialization and execution) and enables access to exported identifiers of that package. The import names an identifier (PackageName) to be used for access and an ImportPath that specifies the package to be imported.

        ImportDecl       = "import" ( ImportSpec | "(" { ImportSpec ";" } ")" ) .
        ImportSpec       = [ "." | PackageName ] ImportPath .
        ImportPath       = string_lit .
    

    The PackageName is used in qualified identifiers to access exported identifiers of the package within the importing source file. It is declared in the file block. If the PackageName is omitted, it defaults to the identifier specified in the package clause of the imported package. If an explicit period (.) appears instead of a name, all the package's exported identifiers declared in that package's package block will be declared in the importing source file's file block and must be accessed without a qualifier.

    The interpretation of the ImportPath is implementation-dependent but it is typically a substring of the full file name of the compiled package and may be relative to a repository of installed packages.

    Implementation restriction: A compiler may restrict ImportPaths to non-empty strings using only characters belonging to Unicode's L, M, N, P, and S general categories (the Graphic characters without spaces) and may also exclude the characters !"#$%&'()*,:;<=>?[]^`{|} and the Unicode replacement character U+FFFD.

    Assume we have compiled a package containing the package clause package math, which exports function Sin, and installed the compiled package in the file identified by "lib/math". This table illustrates how Sin is accessed in files that import the package after the various types of import declaration.

        Import declaration          Local name of Sin
    
        import   "lib/math"         math.Sin
        import m "lib/math"         m.Sin
        import . "lib/math"         Sin
    

    An import declaration declares a dependency relation between the importing and imported package. It is illegal for a package to import itself, directly or indirectly, or to directly import a package without referring to any of its exported identifiers. To import a package solely for its side-effects (initialization), use the blank identifier as explicit package name:

        import _ "lib/math"
    

    The error

    ./main.go:11: imported and not used: "gitlab.com/myapp/api-auth/middlewares"
    

    says that you have no uses of package middlewares in package main, which is true.

    The error

    ./main.go:42: undefined: AcceptHandler
    

    says that you haven't defined AcceptHandler in package main, which is true.

    "A qualified identifier is an identifier qualified with a package name prefix. A qualified identifier accesses an identifier in a different package, which must be imported."

    For example, in package main, use the qualified identifier middlewares.AcceptHandler, which is a use of import "gitlab.com/myapp/api-auth/middlewares".

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

报告相同问题?

悬赏问题

  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统