我对Golang并不陌生,并试图使一个简单的REST api应用程序正常工作。

最初,一切都很好,因为所有代码都在main包下的同一目录中。

但是,现在我处于一个阶段,需要开始将代码重构为子目录和包。 不幸的是,我无法成功编译该应用程序。

我的GOPATH设置为:~/.workspace
当前应用位于:~/.workspace/src/gitlab.com/myapp/api-auth

这就是我当前的代码组织:

enter image description here

这是我的main.go

这是中间件accept.go之一(其余中间件的构造类似)

这是我从应用程序的根目录运行go build时遇到的错误。


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.

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

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

1
2
3
    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.

1
2
3
4
5
    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:

1
    import _"lib/math"

错误

说您在包main中没有使用包middlewares,这是事实。

错误

表示您尚未在包main中定义AcceptHandler,这是事实。

"合格的标识符是带有包名称前缀的合格标识符。合格的标识符访问不同包中的标识符,必须将其导入。"

例如,在包main中,使用合格的标识符middlewares.AcceptHandler,这是对导入"gitlab.com/myapp/api-auth/middlewares"的使用。