How to persist "package" states in Go?

huangapple go评论82阅读模式
英文:

How to persist "package" states in Go?

问题

我的目标是将所有代码封装在一个模块/包中。

主要包:

package main

import (
  "github.com/zenazn/goji"
  "./routes"
)

func main(){
  routes.Setup()
  goji.Serve()
}

另一个包:

package routes

import "github.com/zenazn/goji"

func Setup() {
    goji.Get("/static", func(w http.ResponseWriter, r *http.Request) {
        fmt.Fprint(w, "static!")
    })
}

我该如何实现这个目标?

英文:

My goal is to encapsulate in one module/package.

Main package:

package main

import (
  "github.com/zenazn/goji"
  "./routes"
)

func main(){
  routes.Setup()
  goji.Serve()
}

And another package:

package routes

import "github.com/zenazn/goji"

func Setup() {
	goji.Get("/static", func(w http.ResponseWriter, r *http.Request) {
		fmt.Fprint(w, "static!")
	})
}

How can I do this?

答案1

得分: 4

在你的示例中,goji是一个包,而不是一个变量。

你不能像这样传递包。
如果你查看goji的GitHub页面上的示例

你只需要在你的Init函数中调用goji.Get,然后在你的main函数中调用goji.Serve。

route.go

package route
import "route"
import "github.com/zenazn/goji"
func Init(){
    goji.Get("/hello/:name", hello)
}

main.go

package main
import "github.com/zenazn/goji"
func main(){
    route.Init()
    goji.Serve()
}
英文:

goji, in your example, is a package. Not a variable.

You cannot pass packages around like this.
If you look at the example on the goji github page

You simply just call goji.Get from your Init function, and goji.Serve from your main

route.go

package route
import "route"
import "github.com/zenazn/goji"
func Init(){
    goji.Get("/hello/:name", hello)
}

main.go

package main
import "github.com/zenazn/goji"
func main(){
    route.Init()
    goji.Serve()
}

答案2

得分: 3

在Go语言中,以大写字母命名的常量、变量、类型和函数可以被导出到其他包中使用。包本身不能直接被程序操作。

如果你想要从其他包直接访问goji包,那么goji包应该导出一个名为goji.Goji的变量。一个更好的解决方案是在包中提供一些函数,允许你注册你的函数/助手。

你也可以从goji包中导出一个函数,例如:

func Set(s string, func(w http.ResponseWriter, r *http.Request)) { ... }

其他包可以使用这个函数:

goji.Set("/static", myFunc)

你遇到的错误"use of package goji without selector"是说你不能在不指定要从包中导出的值的情况下使用包的名称。它期望的是goji.something而不是单独的goji

在Go文件中,init()函数具有特殊的属性,请参考http://golang.org/ref/spec#Program_initialization_and_execution。

英文:

Packages in go export constants, variables, types and functions that have uppercase letters as their name. The package itself is not something directly manipulatable by the program.

The package goji should be exporting a variable named something like goji.Goji if you want to directly access it from other packages. A better solution is to provide some functions in the package that allow you to register your functions/helpers.

You could also export a function from goji like:

func Set(s string, func(w http.ResponseWriter, r *http.Request)) { ... }

that could be used by other packages:

goji.Set("/static", myFunc)

The error you had "use of package goji without selector" is saying you can't use the name of the package without specifying which exported value you want from the package. It's expecting goji.something not goji by itself.

The function init() inside go files has special properties: see http://golang.org/ref/spec#Program_initialization_and_execution

huangapple
  • 本文由 发表于 2014年9月13日 23:53:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/25824937.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定