douyakan8924 2017-10-09 11:10
浏览 47
已采纳

转:同一包中的未定义函数[重复]

This question already has an answer here:

I've started Go and am trying to follow this tutorial. Everything builds correct but as I try to run it, I get the error that makeRouter() could not be found.

I had a look at some other questions, like this one, and checked the docs, but I couldn't find out whats wrong with my setup.

My folder structure:

.../wattagebazooka/
  |- wattagebazooka.go
  |- router.go
  |- handler.go

Code

wattagebazooka.go

package main

import (
    "database/sql"
    "fmt"
    "log"
    "net/http"
    "runtime"

    _ "github.com/lib/pq"
)

// init sets runtime settings.
func init() {
    // Verbose logging with file name and line number
    log.SetFlags(log.Lshortfile)

    // Use all CPU cores
    runtime.GOMAXPROCS(runtime.NumCPU())
}

var db *sql.DB

func main() {
    db = openDB()
    defer db.Close()

    r := makeRouter()
    http.Handle("/", r)
}

func openDB() *sql.DB {
    dbName := "wattagebazooka"

    db, err := sql.Open("postgres", fmt.Sprintf("user=ts password= dbname=%s host=127.0.0.1", dbName))
    if err != nil {
        log.Fatalf("Error connecting to the %s database as user ts: %v", dbName, err)
    }

    return db
}

router.go

package main

import "github.com/gorilla/mux"

func makeRouter() *mux.Router {
    r := mux.NewRouter()
    r.HandleFunc("/user/me", wrapHandler(userHandler)).Methods("GET")
    r.HandleFunc("/text", wrapHandler(textHandler)).Methods("POST")
    r.HandleFunc("/text/{hash}", wrapHandler(textHashHandler)).Methods("GET")
    return r
}

Build

> Environment:
>   GOROOT=/usr/local/go
>   GOPATH=/Users/ts/Developments/gocode
> Directory: /Users/ts/Developments/gocode/src/github.com/wattagebazooka/wattagebazooka
> Command: /usr/local/go/bin/go build -v
> Output:
github.com/wattagebazooka/wattagebazooka
> Elapsed: 0.708s
> Result: Success

Run

> Environment:
>   GOROOT=/usr/local/go
>   GOPATH=/Users/ts/Developments/gocode
> Directory: /Users/ts/Developments/gocode/src/github.com/wattagebazooka/wattagebazooka
> Command: /usr/local/go/bin/go run -v /Users/ts/Developments/gocode/src/github.com/wattagebazooka/wattagebazooka/wattagebazooka.go
> Output:
command-line-arguments
# command-line-arguments
./wattagebazooka.go:28:7: undefined: makeRouter
> Elapsed: 0.244s
> Result: Error
</div>
  • 写回答

1条回答 默认 最新

  • dongzhuo5185 2017-10-09 11:15
    关注

    When you run your program via go run you need to add all source files to the command.

    In your case, you only have wattagebazooka.go but are missing router.go and handler.go.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP