Go 中的交互式网页 [英] Interactive web pages in Go

查看:24
本文介绍了Go 中的交互式网页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您知道是否可以在 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.

提前致谢.

推荐答案

浏览器不能直接运行 Go 代码.客户端的交互式网页使用不同的技术,例如 HTML、Javascript 和 CSS.

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.

但是,在客户端使用上述语言并在服务器端使用 Go 完成所有操作是一个可行的技术堆栈.

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.

话虽如此,仍然有一些框架允许您用 Go 编写所有内容,它们将您的 Go 代码转换为客户端理解/支持的语言,或者它们为客户端生成与服务器交互的代码-side Go 代码.

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.

对于后者,有Gowut(Go Web UI 工具包)(披露:我是作者).使用 Gowut,您可以在 Go 中编写所有内容(客户端和服务器端),Gowut 即时生成必要的客户端代码,并负责生成的客户端代码和 Go 服务器代码之间的通信.Gowut 有一个现场演示,你可以在这里查看它的功能:Gowut -功能展示

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 创建完全动态的网页,内容被呈现并且无需重新加载页面即可更改.尽管如此,一切都可以简单地使用 Go 代码完成,但您可以使用 HTML/JS/CSS 代码来增加趣味——如果您有冲动或需要这样做的话.

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.

对于前者,有 GopherJSGo 的 WebAssembly 目标在 Go 1.11 中添加.GopherJS 将 Go 代码编译为纯 Javascript 代码,因此它并不是一个真正的 Web 框架,但您可以使用它在 Go 中编写客户端代码.您仍然需要处理服务器端代码以及它们之间的通信.WebAssembly 目标的工作方式与 GopherJS 类似:您编写 Go 代码,这些代码将被编译为能够在浏览器中运行(由浏览器运行)的形式.链接的维基页面包含如何实现/使用的所有详细信息.

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.

标准库中还有一个强大的模板引擎:html/template.尽管模板只在服务器端执行,但借助一些基本的 Javascript 代码和 AJAX 调用(或 websockets),您可以使您的客户端动态和交互.有关示例,请参阅以下问题和答案:

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:

使用模板在 Golang 中创建加载更多按钮

golang 变量更新时动态刷新模板的一部分

在 Javascript 中引用 Go 数组

这篇关于Go 中的交互式网页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆