[英]GOPATH and Go Web Server: 'go run myserver.go'
I'm at the point where want to organize my Go web server into packages. 我正要把Go Web服务器组织到软件包中。 Currently I have everything in a few files and I simply type:
'go run server.go foo.go bar.go'
目前,我将所有内容保存在几个文件中,只需输入: '运行server.go foo.go bar.go'
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.
我研究了GOPATH变量,但它似乎不起作用。
export GOPATH=$HOME/myserver
I moved my files to the src/ subdirectory. 我将文件移到src /子目录。
myserver/src/server.go
myserver/src/foo.go
myserver/src/bar.go
Shouldn't 'go run' search $HOME/myserver/src for all go files? '运行'不应该在 $ HOME / myserver / src中搜索所有go文件吗?
I've tried these examples but they don't work. 我已经尝试过这些示例,但是它们不起作用。
go run server.go; # Doesn't work
go run src/server.go; # Doesn't work
By the way, all files are in 'package main' 顺便说一下,所有文件都在“ package main”中
This info is covered really well on golang.org 该信息在golang.org上的覆盖非常好
Read this about how to write Go code 阅读本关于如何编写Go代码
And this about organizing go code 这与组织go代码有关
Tip: you can run go run *
to run all files in a folder 提示:您可以运行
go run *
以运行文件夹中的所有文件
Your above example should look something like this 您上面的示例应如下所示
$GOPATH=$HOME
The GOPATH should have: GOPATH应该具有:
$GOPATH
src/
pkg/
bin/
$GOPATH/src
is where you would store your source code for each go project $GOPATH/src
是每个go项目的源代码存储位置
$GOPATH/src/myserver
would contain your myserver program $GOPATH/src/myserver
将包含您的myserver程序
cd to $GOPATH/src/myserver
and run go install
and you would now have your myserver
binary located at $GOPATH/bin/myserver
cd到
$GOPATH/src/myserver
并运行go install
,现在您的myserver
二进制文件位于$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 将bin的位置添加到路径
export PATH=$PATH:$GOPATH/bin
然后可以运行myserver
来启动go程序
go run
will look for a file (or wildcard) in your current working directory. go run
将在当前工作目录中查找文件(或通配符)。
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. 如果您想在任何地方运行程序,请使用与
go run
相同的go build
并在适当的位置移动二进制文件,或者更好的是,设置$GOBIN
环境变量并将其添加到$PATH
,然后运行go install *
在您的项目目录中。
It's also probably a good idea to make specific directories for projects instead just dumping it all in $GOPATH/src
为项目创建特定目录而不是仅将其全部转储到
$GOPATH/src
也是一个好主意
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.