dragon0023 2017-03-21 10:59
浏览 8

去包装命名

How do I reference project packages correctly? I have the following folder structure:

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

In main.go I'd like to call my controller action from sbp.controllers. I can't seem to find a way to correctly reference it

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

I tried importing "sbp/controller", or even "github.com/jiewmeng/finance/sbp/controller" but they seem to be auto removed my VSCode, so I think its invalid?

  • 写回答

1条回答 默认 最新

  • duanhongyi2964 2017-03-21 11:03
    关注

    It sounds like your editor is automatically running goimports, which means it will remove unused imports. So simply adding it to your imports section isn't good enough; you also have to use the import.

    It looks like your proper import path would be "github.com/jiewmeng/finance/sbp". Then you need to reference it as well:

    package main
    
    import (
        // other imports
        "github.com/jiewmeng/finance/bp"
    )
    
    func main() {
        // whatever
        http.Handle("/somepath", http.HandlerFunc(sbp.AddInvestment))
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集