问题
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:
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
回答2:
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:
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:
""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:
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,
来源:https://stackoverflow.com/questions/41197376/interactive-web-pages-in-go