0

如何正确引用项目包?我有以下文件夹结构:

/sbp
    /models.go
    /controllers.go
/main.go

main.go我想从sbp.controllers. 我似乎找不到正确引用它的方法

main.go

package main

import (
    "net/http"

    ...
    "github.com/pressly/chi"
)

var sess = session.Must(session.NewSession(&aws.Config{
    Region: aws.String("ap-southeast-1"),
}))
var dynamo = dynamodb.New(sess)

func main() {
    r := chi.NewRouter()
    r.Get("/", GetInvestments) // How do I call sbp.controller.GetInvestments?
    r.Post("/", AddInvestment)
    http.ListenAndServe(":8080", r)
}

sbp/controller.go

package sbp

import (
    "net/http"
    "time"

    "github.com/pressly/chi/render"
)

// AddInvestment Adds an investment
func AddInvestment(w http.ResponseWriter, r *http.Request) {
    ...
}

// GetInvestments Gets list of investments
func GetInvestments(w http.ResponseWriter, r *http.Request) {
    ...
}

我尝试导入“sbp/controller”,甚至“github.com/jiewmeng/finance/sbp/controller”,但它们似乎是自动删除了我的 VSCode,所以我认为它无效?

4

1 回答 1

1

听起来您的编辑器正在自动运行goimports,这意味着它将删除未使用的导入。因此,仅将其添加到您的imports部分是不够的;您还必须使用导入。

看起来您正确的导入路径应该是“github.com/jiewmeng/finance/sbp”。然后你还需要引用它:

package main

import (
    // other imports
    "github.com/jiewmeng/finance/bp"
)

func main() {
    // whatever
    http.Handle("/somepath", http.HandlerFunc(sbp.AddInvestment))
}
于 2017-03-21T11:03:49.547 回答