如何将Golang后端和JavaScript(three.js)前端集成?

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

How to integrate Golang backend and Javascript (three.js) frontend?

问题

我想使用我最喜欢的编程语言Golang编写一个3D应用程序。然而,我不想直接使用原生的OpenGL作为图形前端,因为这需要我承受比我愿意接受的更大的学习曲线。此外,我也不想使用像go:ngine这样的Golang游戏引擎。

经过一些研究,我发现了一个令人惊叹的WebGL JavaScript库three.js,我对它非常感兴趣,以至于我在几个小时内完成了一个入门级的JavaScript课程。我并不是一个真正的Web开发者,所以我想知道我是否可以在Go中编写我的应用程序,但使用three.js作为前端。

一些具体的问题:

  • 是否值得/实用使用JavaScript/three.js作为像这样的Golang服务器的前端?
  • 如果值得,我应该去哪里学习如何将两者整合得干净利落?
  • 如果不是很值得,我还有哪些选择?

谢谢,期待任何回复。

编辑:

你是否计划在Go中进行几何操作,可能实时地使用Websockets与JavaScript层进行通信?

是的,这是我现在的想法。我打算让我的(Golang)程序根据用户提供的几个参数生成顶点和三角形的流。每个“结构”在用户提供参数后一次性生成(我认为这些参数可以在JavaScript前端上使用滑块和输入框来提供)。

这是一个Go程序可能生成的示例,在GNUPlot中绘制的图形:
[![pic][1]][1]
(图片来源:alexozer.com)

如果是这样,你对JavaScript的估计有点低了。你应该在JavaScript中编写大部分应用程序,并将Go用作经典的后端服务层,如用户账户、持久化状态等。

我可以接受这样做,除了以下原因:

  • 核心生成过程基本上依赖于Goroutines和通道
  • 我对Web开发几乎一无所知,包括前端和后端

所以我想询问其中一种可能性的实现细节:

  • 在JS中编写大部分程序,包括生成器,并在Go中使用最小的后端
  • 在Go中编写大部分程序,并将JS仅用作图形前端
  • 还有其他选择吗?
英文:

I would like to write a 3D application using Golang, my favorite programming language. However, I would prefer not to use native OpenGL directly as the graphical frontend as this would entail a larger learning curve than I'm willing to tolerate. Additionally, I don't really want to use a Golang game engine like go:ngine.

After doing some research I found out about the amazing WebGL Javascript library three.js and I was so intrigued that I completed an introductory Javascript course in a few hours. I'm not really a web developer at all, so I'm wondering how practical it would be for me to write my application primarily in Go but with a three.js frontend.

Some specific questions:
<ul>
<li>Is it worthwhile / practical to use Javascript/three.js as a frontend to a Golang server like this?</li>
<li>If it is worthwhile, where can I look to learn how to integrate the two cleanly?</li>
<li>If it isn't really worthwhile, which alternatives do I have?</li>
</ul>

Thanks, any response appreciated.

Edit:

>Do you plan to make operations on geometry in Go, possibly in realtime and communicate with your Javascript layer using Websockets?

Yes, this is what I'm thinking now. I'm intending my (Golang) program to generate streams of vertices and triangles based on a few parameters from the user. Each "structure" is generated all at once after the users supply their parameters (which I see as slider bars + input boxes on a Javascript frontend).

Here's an example of something the Go program might generate, plotted in GNUPlot:
[![pic][1]][1]
<sub>(source: alexozer.com)</sub>

> If this is the case you underestimate Javascript a bit. You should write most of your application in Javascript and use Go as a classical backend service layer like user accounts, persisting state, etc.

I'd be fine with doing that, except for these reasons:
<ul><li>The core generating process fundamentally depends on Goroutines and channels</li>
<li>I know close to nothing about web development, including frontend and backend</li></ul>

So I suppose I'm asking about the implementation details of one of these three possibilities:
<ul><li>Writing most of the program, including the generator, in JS, and a minimal backend in Go</li>
<li>Writing most of the program in Go and using JS just as a graphical frontend</li>
<li>Something else?</li><ul>
[1]: https://i.stack.imgur.com/1rbbx.png

答案1

得分: 3

我建议不要涉及整个Websocket方面,因为这只是一种可能的优化(甚至可能不是)。如果你需要依赖Go的并发模型,那就用Go编写你的组件,然后用Go编写一个Web服务器,该服务器接收来自HTML请求的参数,使用这些参数计算结果,并以JSON格式发送数据回去。在前端,你只需要在用户更改参数时发送这个请求,并且可以立即使用ThreeJS来显示JSON数据。不过,你仍然需要学习一些Web开发和JavaScript。但是,他们说Web是未来的趋势。我认为对于你的情况来说,这是一个可行的方法,因为你的应用程序在请求-结果方面操作得相当严格。

英文:

I recommend leaving out the whole Websocket aspect because it's only a possible optimization (and maybe not even that).

If you need to depend on Go's concurrency model then go for it, write your component in Go, then a Webserver in Go that takes parameters coming from an HTML request, uses them to compute the result and sends back the data in a JSON format.

On the frontend you will then only focus on sending this request when the user changed a parameter and for displaying of the JSON data you can use ThreeJS right away.

You'll still have to learn a bit of web development and Javascript though. But hey, they say the web is the future 如何将Golang后端和JavaScript(three.js)前端集成?

I think it's the way to go for your case because your application operates in a quite strict request-result way.

答案2

得分: 0

回答你的问题取决于你计划在Go组件和Javascript组件之间划定界限的具体位置。根据你描述问题的方式,我感觉你想在Go中编写大部分应用程序,并且只将JS用作显示层?我不建议这样做。

重新表述我的问题:你计划在Go中对几何操作进行实时处理,并使用Websockets与Javascript层进行通信吗?如果是这种情况,你可能低估了Javascript的能力。你应该在Javascript中编写大部分应用程序,并将Go用作传统的后端服务层,例如用户账户、状态持久化等。

如果我知道你具体计划做什么,我或许能给出更具体的答案。

英文:

The answer to your question depends a lot on where exactly you plan to draw the line between your Go component and the Javascript component. From the way you describe the problem I get the feeling that you want to write most of your application in Go and use JS only as a display layer? I wouldn't recommend doing that.

To rephrase my question: Do you plan to make operations on geometry in Go, possibly in realtime and communicate with your Javascript layer using Websockets? If this is the case you underestimate Javascript a bit. You should write most of your application in Javascript and use Go as a classical backend service layer like user accounts, persisting state, etc.

I might be able to give a more specific answer if I know what exactly you plan to do.

huangapple
  • 本文由 发表于 2014年8月24日 07:59:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/25467405.html
匿名

发表评论

匿名网友

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

确定