dongxian3852 2014-09-11 13:20
浏览 35
已采纳

我可以在共享的Web服务器上使用go生成HTML吗?

Normally I use C# for everything, I can create a desktop application with C# and also place a lot of the same c# code in a dll on my shared hosted web server. This saves me a lot of coding time.

Is there any way to this with go? e.g. place some kind of go dll on my hosted web server to generate HTML.

I am aware that go doesn't do dll's, I am also aware that creating a web server with go to listen to port 80 is straight forward. But this is not a solution for a shared web server.

This seems like a brilliant use for go and it surprises me that this might not be possible.

I should mention, it would be nice if the go code didn't have to restart with every http request.

This is how I do it with C#: On the web server I add an aspx page like this:

<%@ Page Language="C#" %>
<%@ Register TagPrefix="MyDLL" Namespace="MyDLL" Assembly="MyDLL"%>
<MyDLL:Web Runat="Server"/>

It simply loads the C# dll which generates HTML based on the http request.

  • 写回答

1条回答 默认 最新

  • douzhicong5965 2014-09-11 13:52
    关注

    First you have to setup IIS to use fastcgi (instructions) then you can simply build a go web app like you normally would but instead of using http.ListenAndServe you use fcgi.Serve

    Example:

    func hello(w http.ResponseWriter, r *http.Request) {
        io.WriteString(w, "Hello, world of FCGI!")
    }
    
    func main() {
        http.HandleFunc("/hello/", hello)
    
        if err := fcgi.Serve(nil, nil); err != nil {
            log.Fatal("fcgi.Serve: ", err)
        }
    }
    

    But keep in mind that there can be multiple instances of the programming running / destroyed all the time, so any temp data (in-memory sessions, etc) should be stored on memcache or temp files on disk or a database.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 树莓派安卓APK系统签名
  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗