GOPATH and Go Web Server: 'go run myserver.go'

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

GOPATH and Go Web Server: 'go run myserver.go'

问题

我现在是你的中文翻译。以下是你要翻译的内容:

我现在想要将我的Go Web服务器组织成包。目前,我把所有的东西都放在几个文件中,然后简单地输入:'go run server.go foo.go bar.go'

我应该如何组织我的文件,以便不需要不断地将文件添加到命令行中。我已经调查了GOPATH变量,但似乎不起作用。

export GOPATH=$HOME/myserver

我把我的文件移动到了src/子目录中。

myserver/src/server.go
myserver/src/foo.go
myserver/src/bar.go

难道'go run'不应该在$HOME/myserver/src中搜索所有的go文件吗?

我尝试了这些示例,但它们都不起作用。

go run server.go; # 不起作用
go run src/server.go; # 不起作用

顺便说一下,所有的文件都在'package main'中。

英文:

I'm at the point where want to organize my Go web server into packages. Currently I have everything in a few files and I simply type: <b>'go run server.go foo.go bar.go'</b>

How do I organize my files so I don't need to keep adding files to the command line. I've investigated the GOPATH variable but it doesn't seem to work.

export GOPATH=$HOME/myserver

I moved my files to the src/ subdirectory.

myserver/src/server.go
myserver/src/foo.go
myserver/src/bar.go

Shouldn't <em>'go run'</em> search $HOME/myserver/src for all go files?

I've tried these examples but they don't work.

go run server.go; # Doesn&#39;t work
go run src/server.go; # Doesn&#39;t work

By the way, all files are in 'package main'

答案1

得分: 2

这些信息在golang.org上有很好的介绍。

阅读这篇文章了解如何编写Go代码。

以及这篇文章了解如何组织Go代码。

提示:你可以运行go run *来运行文件夹中的所有文件。

你上面的示例应该类似于这样:

$GOPATH=$HOME

GOPATH应该包含以下内容:

$GOPATH
    src/
    pkg/
    bin/

$GOPATH/src是你存储每个Go项目源代码的位置。

$GOPATH/src/myserver将包含你的myserver程序。

切换到$GOPATH/src/myserver目录并运行go install,你现在就可以在$GOPATH/bin/myserver找到你的myserver可执行文件了。

将bin目录的位置添加到你的路径中,可以使用export PATH=$PATH:$GOPATH/bin,这样你就可以运行myserver来启动你的Go程序。

英文:

This info is covered really well on golang.org

Read this about how to write Go code

And this about organizing go code

Tip: you can run go run * to run all files in a folder

Your above example should look something like this

$GOPATH=$HOME

The GOPATH should have:

$GOPATH
    src/
    pkg/
    bin/

$GOPATH/src is where you would store your source code for each go project

$GOPATH/src/myserver would contain your myserver program

cd to $GOPATH/src/myserver and run go install and you would now have your myserver binary located at $GOPATH/bin/myserver

Add the location of your bin to your path export PATH=$PATH:$GOPATH/bin and you can run myserver to start your go program

答案2

得分: 0

go run会在当前工作目录中查找一个文件(或通配符)。

如果你想从任何地方运行你的程序,可以使用go build,就像使用go run一样,并将二进制文件移动到适当的位置,或者更好的方法是设置$GOBIN环境变量并将其添加到$PATH中,然后在项目目录中运行go install *

另外,最好为项目创建特定的目录,而不仅仅是将所有内容都倒入$GOPATH/src目录中。

英文:

go run will look for a file (or wildcard) in your current working directory.

If you would like to run your programs from anywhere, either use go build same as you would go run and move the binaries where appropriate or, better yet, set your $GOBIN environment variable and add it your $PATH -- then run go install * in your project directory.

It's also probably a good idea to make specific directories for projects instead just dumping it all in $GOPATH/src

huangapple
  • 本文由 发表于 2014年10月5日 01:49:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/26195497.html
匿名

发表评论

匿名网友

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

确定