在Go中的交互式网页

huangapple go评论91阅读模式
英文:

Interactive web pages in Go

问题

你好!是的,你可以使用Go语言创建交互式的网页。你可以使用Go的Web框架(如Gin、Echo等)来处理HTTP请求和响应,并使用HTML、CSS和JavaScript来构建前端页面。你可以在网页上添加按钮、下拉框等交互元素,并使用JavaScript来处理用户的选择和刷新页面的操作。你可以通过Go的模板引擎来动态生成HTML页面,并在后端处理用户的选择和数据过滤。希望对你有帮助!

英文:

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.

答案1

得分: 8

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

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

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

对于后者,有一个名为Gowut(Go Web UI Toolkit)的框架(声明:我是作者)。使用Gowut,你可以在Go中编写所有内容(包括客户端和服务器端),Gowut会即时生成必要的客户端代码,并负责生成的客户端代码与Go服务器代码之间的通信。你可以查看Gowut的在线演示,了解它的功能:Gowut - 功能展示

Gowut创建完全动态的网页,内容可以在不刷新页面的情况下渲染和更改。尽管一切都可以简单地使用Go代码完成,但如果你有冲动或需要,你也可以使用HTML/JS/CSS代码来增添一些调料。

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

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

https://stackoverflow.com/questions/41136000/creating-load-more-button-in-golang-with-templates/41138344#41138344

https://stackoverflow.com/questions/37118281/dynamically-refresh-a-part-of-the-template-when-a-variable-is-updated-golang/37119014#37119014

https://stackoverflow.com/questions/28751637/referencing-go-array-in-javascript/28753476#28753476

英文:

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:

https://stackoverflow.com/questions/41136000/creating-load-more-button-in-golang-with-templates/41138344#41138344

https://stackoverflow.com/questions/37118281/dynamically-refresh-a-part-of-the-template-when-a-variable-is-updated-golang/37119014#37119014

https://stackoverflow.com/questions/28751637/referencing-go-array-in-javascript/28753476#28753476

答案2

得分: 0

不,你不能使用Go编写交互式的网页。这是因为Go程序在服务器上运行,而不是在Web浏览器中运行。

要实现交互功能,你需要编写JavaScript程序,与服务器上的Go程序进行通信。

Go程序与交互式Web应用有某种关联,但交互性来自JavaScript部分。

英文:

No, you cannot program interactive web pages in Go. This is because Go programs run on the server, not in the web browser.

To do something interactive, you have to write JavaScript programs that communicate with the Go program on the server.

The Go program is somehow involved with the interactive web app, but the interactivity comes from the JavaScript part.

答案3

得分: 0

你可以使用Go语言和Gin Gonic框架来实现这个功能-在这里可以查看简单而方便的文章:https://semaphoreci.com/community/tutorials/building-go-web-applications-and-microservices-using-gin

英文:

You can do that, using power of Go and Gin Gonic framework - check simple and handy article here: https://semaphoreci.com/community/tutorials/building-go-web-applications-and-microservices-using-gin

答案4

得分: 0

不,你不能使用Go编写交互式的网页。这是因为Go程序在服务器上运行,而不是在Web浏览器中运行。

要实现交互性,你需要编写与服务器上的Go程序进行通信的JavaScript程序。

Go程序与交互式Web应用程序有某种关联,但交互性来自JavaScript部分。

英文:

""No, you cannot program interactive web pages in Go. This is because Go programs run on the server, not in the web browser.

To do something interactive, you have to write JavaScript programs that communicate with the Go program on the server.

The Go program is somehow involved with the interactive web app, but the interactivity comes from the JavaScript part. ""

I believe the answer is actually yes Go is capable of creating interactive websites, go is capable of reading from the http.Request so from this input you are able to define through your Go Code what will happen next with some Advanced Go programing.Go itself in theory was designed to be a web server side programming language but of course its output medium is the Web.

答案5

得分: 0

我发现了对我自己问题的长答案。直接使用Go语言无法编写交互式网站,但是使用GopherJS(https://github.com/gopherjs/gopherjs)(它是一个从Go到JS的编译器)和Go语言,非常有可能创建完全交互式的网站。
尽管这是一个高级的任务,并且目前(2018/09/25)没有太多可用的信息。

感谢大家的努力(无论是好是坏)。

谢谢你提供的这部分内容:“对于前者,有GopherJS。GopherJS将Go代码编译为纯JavaScript代码,所以它不是一个真正的Web框架,但你可以使用Go语言编写客户端代码。你仍然需要处理服务器端代码和它们之间的通信。”这帮了很多忙。

英文:

I found the long answer to my own question. It is not possible to directly program an interactive website with only Go BUT using GopherJS(https://github.com/gopherjs/gopherjs) (its a compiler from Go to JS) and Go it is very possible to create fully interactive websites.
Although this undertaking is advanced and there is not allot of information available at this moment(2018/09/25)

I thank you all for your efforts (good and bad).

thank you for this part "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." Helped allot,

huangapple
  • 本文由 发表于 2016年12月17日 17:56:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/41197376.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定