1.test.go文件,使用cgi包,.go不是可执行的文件,使用 go run 来调用
package main
import (
"log"
"net/http"
"net/http/cgi"
)
func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
handler := new(cgi.Handler)
handler.Path = "D:/Go/bin/go"
script := "D:/workspace/goPro/" + r.URL.Path
log.Println(handler.Path)
handler.Dir = "D:/workspace/goPro/"
args := []string{"run", script}
handler.Args = append(handler.Args, args...)
handler.Env = append(handler.Env, "GOPATH=D:/Go/gopath")
handler.Env = append(handler.Env, "GOROOT=D:/Go")
log.Println(handler.Args)
handler.ServeHTTP(w, r)
})
log.Fatal(http.ListenAndServe(":8080", nil))
}
2.testcgi.go代码
package main
import (
"fmt"
)
func init() {
fmt.Print("Content-Type: text/plain;charset=utf-8\n\n")
}
func main() {
fmt.Println("This is gocgi test")
}
3.浏览器输入 http://127.0.0.1:8080/testcgi.go 查看运行结果
参考原文链接地址为: http://www.cnblogs.com/yjf512/archive/2012/12/25/2831891.html
我这里使用自己的环境进行测试运行并通过