duanfu7840 2016-12-17 09:56
浏览 70
已采纳

Go中的交互式网页

Do you know if it's possible to create interactive web pages in Go? For example, having one or multiple buttons, or a combo box that refreshes the page with the data being filtered according to the choice? I've tried to look for it but didn't find anything relevant.

Thanks in advance.

  • 写回答

5条回答 默认 最新

  • dsjpik057730 2016-12-17 12:33
    关注

    Browsers are not capable of running Go code directly. Interactive web pages at the client side use different technologies, such as HTML, Javascript and CSS.

    However, it is a viable technology stack to use the above mentioned languages at the client side, and do everything in Go at the server side.

    That being said, there are still some frameworks that allow you to write everything in Go, and they transform your Go code to languages understood / supported by the client side, or they generate code for the client side that interact with the server-side Go code.

    For the latter, there is Gowut (Go Web UI Toolkit) (disclosure: I'm the author). With Gowut, you can write everything in Go (both the client and the server side), and Gowut generates the necessary client code on-the-fly, and it takes care of the communication between the generated client code and the Go server code. There is a live demo of Gowut, you can check it out and see what it is capabe of here: Gowut - Showcase of Features

    Gowut creates completely dynamic webpages, the content is rendered and can change without page reload. And still, everything can be done simply using Go code, but you have the possibility to use HTML / JS / CSS code to spice things up – should you have the urge or need to do so.

    For the former, there's GopherJS and Go's WebAssembly target added in Go 1.11. GopherJS compiles Go code to pure Javascript code, so it's not really a web framework, but you can write client side code in Go with that. You still have to take care of server-side code and the communication between them. The WebAssembly target works similarly to GopherJS: you write Go code which will be compiled to a form that is capable running in the browser (run by the browser). The linked wiki page holds all the details how this can be achieved / used.

    There is also a powerful template engine in the standard library: html/template. Although templates are executed exclusively at the server side, with the help of some basic Javascript code and AJAX calls (or websockets), you can make your client side dynamic and interactive. For examples, see these questions and answers:

    Creating load more button in Golang with templates

    Dynamically refresh a part of the template when a variable is updated golang

    Referencing Go array in Javascript

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条