获取子目录中的函数访问权限

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

Getting access to functions from subdirectories

问题

我正在编写一个小应用程序,遵循http://golang.org/doc/code.html的指南。

我的目录结构如下:

-blog
  -bin
  -pkg
  -src
    -github.com
      -我导入的包
    -myblog
      -config
        routes.go
      server.go

我的server.go文件包含以下代码:

package main

import "..." //省略的导入语句


func main(){
    r := mux.InitRoutes()  //来自导入包的函数
    Register_routes(r)  //来自routes.go的函数
}

我的routes.go文件如下:

package main

func Register_routes(r *Router){
    r.addRoute("test", "test", "test")
}

但是当我运行go run server.go时,我得到以下错误:

$ go run server.go 
# command-line-arguments
./server.go:10: undefined: Register_routes

GOPATH变量指向我的/blog文件夹。

我漏掉了什么?为什么Go看不到子目录中的文件?

附注:config/routes.go是server.go包的一部分。

附注:我已经将routes.go移动到与server.go相同的文件夹中,但错误仍然存在。

英文:

I'm writing small app following http://golang.org/doc/code.html

My directory tree looks like

-blog
  -bin
  -pkg
  -src
    -github.com
      -packages_that_i_imported
    -myblog
      -config
        routes.go
      server.go

my server.go file contains following code

package main

import "..." //ommited imports


func main(){
	r:= mux.InitRoutes()  //function from imported package
	Register_routes(r)  //function from routes.go
}

And my routes.go

package main

func Register_routes(r *Router){
  r.addRoute("test", "test", "test)
}

But after I do go run server.go
I'm getting following error

$ go run server.go 
# command-line-arguments
./server.go:10: undefined: Register_routes

GOPATH variable points to my /blog folder

What am I missing? Why go doesn't it see files in subdirectories?

P.S. config/routes.go is part of server.go package

P.P.S I have moved routes.go to the same folder as server.go, but the error is still present

答案1

得分: 3

为了使用另一个包中定义的函数,首先你需要导入它:

import "myblog/config"

之后你需要通过包名来引用它:

config.Register_routes(r)

同时,包名应该反映出它所定义的文件夹名。在你的routes.go文件中,包名应该是configmain包是特殊的,main包会被编译成可执行二进制文件(它是程序的入口点)。请参阅语言规范中的程序执行部分。

从你提供的链接中可以看到:包名

Go的约定是包名是导入路径的最后一个元素:导入为"crypto/rot13"的包应该命名为rot13

可执行命令必须始终使用main包。

包名在单个二进制文件中不需要唯一,只需要导入路径(它们的完整文件名)唯一。

请查看博客文章包名以获取详细的指南。

请注意,同一个包的不同文件必须放在同一个文件夹中。同一个包的不同文件可以在不导入和不使用包名的情况下使用该包中的所有内容(无论它在哪个文件中定义)。这对于未导出的标识符也是适用的。从另一个包中,你只能访问导出的标识符(它们的名称必须以大写字母开头)。

另外,Go的命名约定是使用驼峰命名法而不是下划线来编写多个单词的名称,参见Effective Go / MixedCaps。所以这个函数应该命名为RegisterRoutes,但这不是必需的。

英文:

In order to use a function defined in another package, first you have to import it:

import "myblog/config"

And after that you have to refer to it by the package name:

config.Register_routes(r)

Also the package name should reflect the folder name in which it is defined. In your routes.go the package should be config. Package main is special, the main package will be compiled into an executable binary (it is the entry point of the program). See Program Execution in the language specification.

From the page you linked: Package names:

> Go's convention is that the package name is the last element of the import path: the package imported as "crypto/rot13" should be named rot13.
>
> Executable commands must always use package main.
>
> There is no requirement that package names be unique across all packages linked into a single binary, only that the import paths (their full file names) be unique.

Check out the blog post Package names for a detailed guideline.

Note that different files of the same package have to be put into the same folder. And different files of the same package can use everything from the package without importing it and without using the package name (doesn't matter in which file it is defined). This is also true for unexported identifiers. From another package you can only access exported identifiers (their name must start with a capital letter).

Also the go naming convention is to used mixed caps rather than underscores to write multiword names, see Effective Go / MixedCaps. So the function should be named RegisterRoutes but this is not a requirement.

huangapple
  • 本文由 发表于 2015年6月1日 19:18:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/30572146.html
匿名

发表评论

匿名网友

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

确定