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

问题描述

你知道是否可以在Go中创建交互式网页?
例如,有一个或多个按钮,还是一个组合框,可以刷新页面,并根据选择过滤数据?
我试图寻找它,但没有找到任何相关的东西。



预先致谢。

解决方案

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

然而,在上述语言中使用上述语言是一个可行的技术栈客户端,并在服务器端执行一切。



这就是说,仍然有一些框架允许您在Go中编写所有内容,并且它们将您的将代码转换为客户端理解/支持的语言,或者为客户端生成与服务器端Go代码交互的代码。



对于后者,那里是(披露:I作者)。使用Gowut,您可以在Go中编写所有内容(客户端和服务器端),并且Gowut可以即时生成必要的客户端代码,并且负责生成的客户端代码与Go服务器代码之间的通信。有一个Gowut现场演示,你可以看看它的功能:



Gowut创建完全动态的网页,内容呈现并可在没有页面重新加载的情况下更改。尽管如此,只需使用Go代码就可以完成所有工作,但是您可以使用HTML / JS / CSS代码来增强功能 - 如果您有这种需求或需要这样做。



前者有。 GopherJS将Go代码编译为纯Javascript代码,所以它不是一个真正的Web框架,但是您可以用Go编写客户端代码。您仍然需要处理服务器端代码以及它们之间的通信。



标准库中还有一个功能强大的模板引擎:。虽然模板只在服务器端执行,但在一些基本的Javascript代码和AJAX调用(或websockets)的帮助下,您可以使您的客户端动态和交互。例如,请参阅以下问题和答案:

$ b


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.

解决方案

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. 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.

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

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

08-29 04:07