dsfdsf23423 2014-07-08 19:37
浏览 79
已采纳

从go程序中的其他文件访问对象

The goji microframework for Go has a fully functional example app with three files, main.go, models.go and middleware.go. I installed the framework using the go get command

go get github.com/zenazn/goji

and therefore have the example app availabl in my GOPATH like this

src/github.com/zenazn/goji/example

if I navigate to /example/ and run go run main.go, it gives me errors which indicate that the main.go file is isn't accessing the objects from the middleware.go and models.go files, like this

./main.go:39: undefined: PlainText
./main.go:47: undefined: SuperSecure
./main.go:73: undefined: Greets
./main.go:74: undefined: Greets
./main.go:85: undefined: Greet
./main.go:98: undefined: Greets
./main.go:99: undefined: Greets
./main.go:107: undefined: Users
./main.go:116: undefined: Greets
./main.go:116: too many errors

There is no code in main.go that imports middleware.go or models.go, only the regular import statements for libraries.

How are these files supposed to be tied together so that the objects from one are available in the other?

from main.go

package main

import (
    "fmt"
    "io"
    "net/http"
    "regexp"
    "strconv"

    "github.com/zenazn/goji"
    "github.com/zenazn/goji/param"
    "github.com/zenazn/goji/web"
)

// Note: the code below cuts a lot of corners to make the example app simple.

func main() {
    // Add routes to the global handler
    goji.Get("/", Root)
    // Fully backwards compatible with net/http's Handlers
    goji.Get("/greets", http.RedirectHandler("/", 301))
    // Use your favorite HTTP verbs
    goji.Post("/greets", NewGreet)
    // Use Sinatra-style patterns in your URLs
    goji.Get("/users/:name", GetUser)
    // Goji also supports regular expressions with named capture groups.
    goji.Get(regexp.MustCompile(`^/greets/(?P<id>\d+)$`), GetGreet)

    // Middleware can be used to inject behavior into your app. The
    // middleware for this application are defined in middleware.go, but you
    // can put them wherever you like.
    goji.Use(PlainText)

    admin := web.New()
    goji.Handle("/admin/*", admin)
    admin.Use(SuperSecure)

    // Goji's routing, like Sinatra's, is exact: no effort is made to
    // normalize trailing slashes.
    goji.Get("/admin", http.RedirectHandler("/admin/", 301))


    admin.Get("/admin/", AdminRoot)
    admin.Get("/admin/finances", AdminFinances)

    // Use a custom 404 handler
    goji.NotFound(NotFound)

    goji.Serve()
}

middleware.go

package main

import (
    "encoding/base64"
    "net/http"
    "strings"

    "github.com/zenazn/goji/web"
)

// PlainText sets the content-type of responses to text/plain.
func PlainText(h http.Handler) http.Handler {
    fn := func(w http.ResponseWriter, r *http.Request) {
        w.Header().Set("Content-Type", "text/plain")
        h.ServeHTTP(w, r)
    }
    return http.HandlerFunc(fn)
}

// Nobody will ever guess this!
const Password = "admin:admin"

// SuperSecure is HTTP Basic Auth middleware for super-secret admin page. Shhhh!
func SuperSecure(c *web.C, h http.Handler) http.Handler {
    fn := func(w http.ResponseWriter, r *http.Request) {
        auth := r.Header.Get("Authorization")
        if !strings.HasPrefix(auth, "Basic ") {
            pleaseAuth(w)
            return
        }

        password, err := base64.StdEncoding.DecodeString(auth[6:])
        if err != nil || string(password) != Password {
            pleaseAuth(w)
            return
        }

        h.ServeHTTP(w, r)
    }
    return http.HandlerFunc(fn)
}

func pleaseAuth(w http.ResponseWriter) {
    w.Header().Set("WWW-Authenticate", `Basic realm="Gritter"`)
    w.WriteHeader(http.StatusUnauthorized)
    w.Write([]byte("Go away!
"))
}

models.go

package main

import (
    "fmt"
    "io"
    "time"
)

// A Greet is a 140-character micro-blogpost that has no resemblance whatsoever
// to the noise a bird makes.
type Greet struct {
    User    string    `param:"user"`
    Message string    `param:"message"`
    Time    time.Time `param:"time"`
}

// Store all our greets in a big list in memory, because, let's be honest, who's
// actually going to use a service that only allows you to post 140-character
// messages?
var Greets = []Greet{
    {"carl", "Welcome to Gritter!", time.Now()},
    {"alice", "Wanna know a secret?", time.Now()},
    {"bob", "Okay!", time.Now()},
    {"eve", "I'm listening...", time.Now()},
}

// Write out a representation of the greet
func (g Greet) Write(w io.Writer) {
    fmt.Fprintf(w, "%s
@%s at %s
---
", g.Message, g.User,
        g.Time.Format(time.UnixDate))
}

// A User is a person. It may even be someone you know. Or a rabbit. Hard to say
// from here.
type User struct {
    Name, Bio string
}

// All the users we know about! There aren't very many...
var Users = map[string]User{
    "alice": {"Alice in Wonderland", "Eating mushrooms"},
    "bob":   {"Bob the Builder", "Making children dumber"},
    "carl":  {"Carl Jackson", "Duct tape aficionado"},
}

// Write out the user
func (u User) Write(w io.Writer, handle string) {
    fmt.Fprintf(w, "%s (@%s)
%s
", u.Name, handle, u.Bio)
}
  • 写回答

2条回答 默认 最新

  • dongxifu5009 2014-07-08 19:44
    关注

    Just use go run or go run *.go you are running/compiling only main.go the other files are not included.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥85 maple软件,solve求反函数,出现rootof怎么办?
  • ¥15 求chat4.0解答一道线性规划题,用lingo编程运行,第一问要求写出数学模型和lingo语言编程模型,第二问第三问解答就行,我的ddl要到了谁来求了
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题