通过“即时编译”从源代码在Apache下运行一个.go文件。

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

Run a .go file under Apache from source by 'compiling on the fly'

问题

我能够使用以下代码将Go应用程序作为网站在Apache上运行。

hello.go:

package main

import (
    "os"
)

func main() {
    os.Stdout.WriteString("Content-Type: text/html; charset=UTF-8\n\n")
    os.Stdout.WriteString("Hello!\n")
}

.htaccess:

AddHandler cgi-script .exe

我使用go build hello.go编译应用程序,并且访问http://localhost/hello.exe可以正常工作。

但是现在,每次我在源代码中进行更改后,都必须重新编译。

有没有办法告诉Apache在访问http://localhost/hello.go时运行hello.go(Apache应该运行go run hello.go)?

顺便说一句,这只是为了加快开发速度,不用于生产环境!

英文:

I'm able to run a Go application as a website with Apache using the following code.

hello.go:

package main

import (
	"os"
)

func main() {
	os.Stdout.WriteString("Content-Type: text/html; charset=UTF-8\n\n")
	os.Stdout.WriteString("Hello!\n")
}

.htaccess:

AddHandler cgi-script .exe

I compile the app using go build hello.go and going to http://localhost/hello.exe works as expected.

But now I have to recompile after every change I make in the sourcecode.

Is it somehow possible to tell Apache to run hello.go (Apache should run go run hello.go) when visiting http://localhost/hello.go?

By the way, this is only to speed development, not for production!

答案1

得分: 3

Go是一种编译语言,你需要先编译它。目前还没有任何Go的解释器/虚拟机。

你最好的选择是创建一个进程/定时任务,检查.go文件是否比二进制文件更新,并在发现文件变化时重新构建它。

https://github.com/howeyc/fsnotify 是一个允许你监视文件变化的包。

英文:

Go is a compiled language, you'd need to compile it first. There currently aren't any interpreters/VM's for Go.

You're best bet is to just have a process/cron job that checks for the .go file being newer than the binary, and rebuilding it when it notices the file changed.

https://github.com/howeyc/fsnotify is a package that allows you to watch files for changes.

答案2

得分: 2

你可以使用gorun,它允许你在文件顶部加上#!/usr/bin/gorun这一行,使其像脚本一样运行。gorun会即时编译并运行它。我经常用它来创建golang脚本,我认为它也适用于CGI。

你需要将脚本标记为可执行(chmod +x),并告诉apache .go扩展名是可执行的。

不确定我是否推荐在生产环境中使用,但它应该相当高效,因为gorun有一个缓存。

英文:

You could use gorun which enables you to put a #!/usr/bin/gorun line at the top of the file so it is run like a script. gorun will compile it on the fly then run it. I've used it quite a bit for making golang scripts and I expect it would work for CGIs too.

You'd have to mark the script as executable (chmod +x) and tell apache that the .go extension was executable.

Not sure I'd recommend this for production but it should work reasonably efficiently as gorun has a cache.

答案3

得分: 1

一个简单的解决方案是使用一个工具,在源文件发生变化时重新编译你的代码。例如GoWatch

或者你可以尝试使用fsnotify,就像Erik已经提到的那样。例如:简单的编译守护进程

你也可以在你的CGI脚本中调用go run

英文:

An easy solution would be to use a tool which re-compiles your code on changes to the source files. For example GoWatch.

Or try it yourself by using fsnotify as Erik already stated. Example: Simple Compile Daemon.

You could also invoke go run in your CGI script.

huangapple
  • 本文由 发表于 2012年10月19日 22:45:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/12976688.html
匿名

发表评论

匿名网友

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

确定