douweng3383
2018-04-12 19:56
浏览 52
已采纳

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

I'm testing a simple server app with Gorilla Mux. I keep getting undefined error when running the app. This is the structure of the app

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))
}

As you can see, I have a main that initialized app by importing from ptest/app. But I am getting an error when I go run *go:

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

This is my go env. I am wondering if something not right with my environment?

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"

图片转代码服务由CSDN问答提供 功能建议

我正在使用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应用程序结构{
路由器* mux.Router 
} 
 
func(A * App)Run()  {
 fmt.Println(“监听于:8080”)
 log.Fatal(http.ListenAndServe(“:8080”,A.Router))
} 
   
 \  n 

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

 #command-line-arguments 
./main.go:5时,出现错误:  2:导入而未使用:“ ptest / app” 
./main.go:10:9:未定义:App 
   
 
 

这是我的 进入环境。 我想知道我的环境是否不正确?

  GOARCH =“ amd64” 
GOBIN =“” 
GOCACHE =“ / Users / haha​​ / Library / Caches / go- 打造 “
GOEXE =” “
GOHOSTARCH =” AMD64 “
GOHOSTOS =” 达尔文 “
GOOS =” 达尔文 “
GOPATH =”/用户/哈哈/去 “
GORACE =” “
GOROOT =” 在/ usr /本地/ 去 “
GOTMPDIR =” “
GOTOOLDIR =”/ USR /本地/去/包装/工具/ darwin_amd64 “
GCCGO =” gccgo “\ NCC =” 铛 “
CXX =” 铛++ “
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” 
 <  / code>  
 
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • doumen1883 2018-04-12 19:58
    已采纳

    Use App{} struct by package name. Your are importing the package but not using it. App struct is declared in app package. that's why the error.

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

    In your program you are trying to initialize an App{} which does not exist in main.go.

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

    It is well described in Golang Spec for 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
    
    点赞 评论

相关推荐 更多相似问题