我已经开始Go了,我正试图遵循这个tutorial。一切都构建正确,但是当我尝试运行它时,我收到makeRouter()
无法找到的错误。
我查看了其他一些问题like this one,并查看了文档,但我发现我的设置错误了。
.../wattagebazooka/
|- wattagebazooka.go
|- router.go
|- handler.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
}
> 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
> 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
答案 0 :(得分:4)
当您通过go run
运行程序时,需要将所有源文件添加到命令中。
在您的情况下,您只有wattagebazooka.go
但却缺少router.go
和handler.go
。