如何集成Golang后端和Javascript(three.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:

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

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
(source: alexozer.com)

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:

  • The core generating process fundamentally depends on Goroutines and channels
  • I know close to nothing about web development, including frontend and backend

So I suppose I'm asking about the implementation details of one of these three possibilities:

Writing most of the program, including the generator, in JS, and a minimal backend in Go Writing most of the program in Go and using JS just as a graphical frontend Something else?

2个回答



我建议忽略整个Websocket方面,因为这只是可能的优化(也许甚至不是那样)。</ p>

如果您需要依赖Go的并发模型,请继续使用它,在Go中编写您的组件,然后在Go中使用一个Web服务器,该服务器获取来自HTML请求的参数,使用它们来计算结果并发送回数据 </ p>

在前端,您将仅专注于在用户更改参数时发送此请求,并且可以立即使用ThreeJS来显示JSON数据。</ p>

p>

尽管如此,您仍然必须学习一些Web开发和Javascript。 但是,嘿,他们说网络是未来;-)</ p>

我认为这是解决问题的方法,因为您的应用程序以非常严格的请求结果方式运行。</ p >
</ div>

展开原文

原文

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 ;-)

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

doujiang1832
doujiang1832 这就是您所需要的。 确保您专注于核心问题。 首先尝试简单的ThreeJS设置。 然后看看如何通过Javascript向简单的Go服务器发出基本AJAX请求。
大约 6 年之前 回复
dongxieyou3314
dongxieyou3314 听起来像一个合理的计划; 我不介意(最终)学习一些Web开发。 您是否认为HTML,CSS和Javascript可以将其用于类似这样的事情,还是我需要关注更一般的事情?
大约 6 年之前 回复



问题的答案在很大程度上取决于您计划在Go组件和Javascript组件之间划界的确切位置。 从描述问题的方式来看,我感觉到您想用Go编写大部分应用程序,而仅将JS用作显示层? 我不建议这样做。</ p>

要重新说明我的问题:您是否打算在Go中对几何进行实时操作,并使用Websockets与Javascript层进行通信? 如果是这种情况,您可能会低估Javascript。 您应该使用Javascript编写大多数应用程序,并将Go用作经典的后端服务层,例如用户帐户,持久状态等。</ p>

如果我可以给出更具体的答案, 知道您打算做什么。</ p>
</ div>

展开原文

原文

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.

doubianyu7844
doubianyu7844 根据您的输入更新了我的问题
大约 6 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐