golang cgi运行

benlightning · · 6441 次点击 · · 开始浏览    
这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。

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

我这里使用自己的环境进行测试运行并通过


有疑问加站长微信联系(非本文作者)

本文来自:博客园

感谢作者:benlightning

查看原文:golang cgi运行

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

6441 次点击  
加入收藏 微博
上一篇:Go基本配置(2)
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传