Golang Gorilla Mux 服务器本地包导入和未使用问题

Golang Gorilla Mux server local package import and not used issue

我正在使用 Gorilla Mux 测试一个简单的服务器应用程序。 运行 应用程序时,我一直收到未定义的错误。这是应用程序的结构

src/ptest/
├── app
│   └── app.go
└── main.go

main.go

package main

import (
    "fmt"
    "ptest/app"
)

func main() {
    fmt.Println("Hello Testing App")
    app := App{}
}

app.go

package app

import (
    "fmt"
    "log"
    "net/http"
    "github.com/gorilla/mux"
)

type App struct {
    Router *mux.Router
}

func (A *App) Run() {
    fmt.Println("Listening at :8080")
    log.Fatal(http.ListenAndServe(":8080", A.Router))
}

如您所见,我有一个 main 通过从 ptest/app 导入来初始化 app。但是当我 go run *go:

时出现错误
# command-line-arguments
./main.go:5:2: imported and not used: "ptest/app"
./main.go:10:9: undefined: App

这是我的go env。我想知道我的环境是否有问题?

GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/haha/Library/Caches/go-build"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/haha/go"
GORACE=""
GOROOT="/usr/local/go"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
GCCGO="gccgo"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"

按包名使用App{}结构。您正在导入包但未使用它。 App 结构在 app 包中声明。这就是错误的原因。

# command-line-arguments
./main.go:5:2: imported and not used: "ptest/app"
./main.go:10:9: undefined: App

在您的程序中,您正试图初始化一个 App{},它在 main.go 中不存在。

package main

import (
    "fmt"
    "ptest/app"
)

func main() {
    fmt.Println("Hello Testing App")
    app := app.App{}
}

在 Golang 规范中对 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