我的main.go文件无法看到其他文件。

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

My main.go file cannot see other files

问题

我需要帮助理解一个简单的Web应用程序中文件布局的问题。

$GOPATH/src/example.com/myweb

然后我有两个文件:

$GOPATH/src/example.com/myweb/main.go
$GOPATH/src/example.com/myweb/api.go

这两个文件都有:

package main

api.go文件的内容如下:

package main

import (
	"bytes"
	"encoding/json"
	"fmt"
	"io"
	"net/http"
	"time"
)

type API struct {
	URI    string
	Token  string
	Secret string
	client *http.Client
}

...

我的main.go文件的内容如下:

package main

import (
	"github.com/gorilla/mux"
	"html/template"
	"net/http"
)

var (
	templates = template.Must(template.ParseFiles("views/home.html", "views/history.html", "views/incident.html"))
	api = API{
		URI:    "http://localhost:3000",
		Token:  "abc",
		Secret: "123",
	}
)

func renderTemplate(w http.ResponseWriter, tmpl string, hp *HomePage) {
..
..
}

func WelcomeHandler(w http.ResponseWriter, r *http.Request) {
..
..
}

func main() {
	r := mux.NewRouter()
	r.HandleFunc("/", WelcomeHandler)
	r.PathPrefix("/assets/").Handler(
		http.StripPrefix("/assets/", http.FileServer(http.Dir("assets/"))))

	http.ListenAndServe(":9000", r)
}

在我省略的代码中,我基本上使用了在api.go文件中定义的结构体,但是当我运行以下命令时出现了错误:

go run main.go
# command-line-arguments
./main.go:16: undefined: API
./main.go:23: undefined: User

我在这里到底做错了什么?

我尝试将api.go中的包名更改为myweb,但没有帮助。

我应该使用包名myweb吗?只有一个文件应该有main吗?

英文:

I need some help understanding what is wrong with my file layout in a simple web application.

$GOPATH/src/example.com/myweb

I then have 2 files:

$GOPATH/src/example.com/myweb/main.go
$GOPATH/src/example.com/myweb/api.go

Both files have:

package main

The api.go file looks like:

package main

import (
	"bytes"
	"encoding/json"
	"fmt"
	"io"
	"net/http"
	"time"
)

type API struct {
	URI   string
	Token  string
	Secret string
	client *http.Client
}

...

My main.go file looks like:

package main

import (
	"github.com/gorilla/mux"
	"html/template"
	"net/http"
)

var (
	templates = template.Must(template.ParseFiles("views/home.html", "views/history.html", "views/incident.html"))
	api = API{
		URI: "http://localhost:3000",
		Token: "abc",
		Secret: "123",
	}
)

func renderTemplate(w http.ResponseWriter, tmpl string, hp *HomePage) {
..
..
}

func WelcomeHandler(w http.ResponseWriter, r *http.Request) {
..
..
}

func main() {
	r := mux.NewRouter()
	r.HandleFunc("/", WelcomeHandler)
	r.PathPrefix("/assets/").Handler(
		http.StripPrefix("/assets/", http.FileServer(http.Dir("assets/"))))

	http.ListenAndServe(":9000", r)
}

In the code I excluded, I basically use structs that are defined in my api.go file, and I get this error when doing:

go run main.go
# command-line-arguments
./main.go:16: undefined: API
./main.go:23: undefined: User

What exactly am I doing wrong here?

I tried changing the package name in api.go to myweb but that didn't help.

Am I suppose to use the package name myweb? Is just 1 file suppose to have main?

答案1

得分: 4

你只编译了main.go文件。你应该使用以下命令:

go run main.go api.go

或者:

go run *.go

如果你正在编写一个复杂的应用程序,你可以将所有内容添加到子目录的包中,并有一个单独的main.go文件。例如,etcd有一个etcdmain子目录/包以及其他子目录/包。类似于:

/alarm    
/auth
/cmd
/etcdmain
...

而main.go文件只需包含以下内容:

package main

import "github.com/coreos/etcd/etcdmain"

func main() {
    etcdmain.Main()
}
英文:

You're compiling only the main.go file. You should use:

go run main.go api.go

Or:

go run *.go

If you're writing a complex application, you might add everything to packages in subdirectories and have a single main.go file. For instance, etcd has an etcdmain subdirectory/package along with other subdirectories/packages. Something like:

/alarm    
/auth
/cmd
/etcdmain
...

And the main.go file is simply:

package main

import "github.com/coreos/etcd/etcdmain"

func main() {
	etcdmain.Main()
}

答案2

得分: 1

你正在使用golang工作区项目,这对于你的应用程序的结构和标准化都是很好的。

当我们使用golang工作区时,不能运行单个go文件。你需要调用go build / go install。

安装

go install example.com/myweb

上述命令将编译example.com/myweb的主包。myweb可执行二进制文件将被放置在GOPATH/bin目录中。然后你可以手动运行它。

构建

go build example.com/myweb

该命令与go install类似,但是当你调用该命令时,二进制可执行文件将被放置在当前目录中,而不是GOPATH/bin(除非你的当前目录是GOPATH/bin)。

更多信息请查看此链接

英文:

You are using golang workspace project, which is good for the structure for your application and it also standardize.

When we use the golang workspace, you can not run single go file. You need to call go build / go install.

Install

go install example.com/myweb

The command above will compile your main package on example.com/myweb. And the myweb executable binary will be placed on the GOPATH/bin. And you can run it manually.

Build

go build example.com/myweb

The command is similar to go install but the binary executable file will be placed on the current directory when you call the command, instead of on GOPATH/bin (unless your current directory is GOPATH/bin).

For more information please check this link.

huangapple
  • 本文由 发表于 2016年4月17日 08:57:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/36671520.html
匿名

发表评论

匿名网友

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

确定