Is it possible to open a window, draw to it and register events from it with native Go code?

blov · · 468 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>Hi, I&#39;ve been wondering about this for some time, but looking through the standard library package list as well as some of the packages, I&#39;ve failed to find a way to do it.</p> <p>I&#39;m beginning to doubt whether it&#39;s possible, which would explain why all the gui libraries I&#39;ve seen rely on cgo, GTK or stuff like that.</p> <p>If it is possible, where should I look for resources on how to do it? If not, then why isn&#39;t it possible?</p> <hr/>**评论:**<br/><br/>TheMerovius: <pre><p>You should specify what operating system, display server and the like you are talking about.</p> <p>For X11 under linux there is <a href="https://github.com/BurntSushi/xgb" rel="nofollow">xgb</a>, which can do, what you want. Shiny (experimental GUI toolkit for go) has a <a href="https://godoc.org/golang.org/x/exp/shiny/driver/x11driver" rel="nofollow">driver that uses it</a>.</p> <p>In general, go can do everything, that C can do (there are some limitations around use of the <code>clone()</code> library call due to the way scheduling works in the runtime, but even they can be worked around), so, everything is <em>possible</em>. In the end, C code is also just user-level code that uses the syscall-interface of the operating system (usually wrapped by a standard library). There is no fundamental difference. It&#39;s just that, for C, most of the work has already been done in the form of C libraries that you can use. Part of the reason is, that most languages can use C libraries, so you can avoid duplication while still doing the brunt of your work in a high-level language. This is, why so many GUI toolkits use C libraries; because they are there and you don&#39;t need to reimplement them in go. It&#39;s not that it&#39;d be <em>impossible</em> to do.</p></pre>neoasterisk: <pre><p>Your best bet is probably <a href="https://github.com/golang/exp/tree/master/shiny" rel="nofollow">shiny</a>. Take a look at the examples folder. Also for something more complete and complex there is the <a href="https://github.com/driusan/de" rel="nofollow">de editor</a>. </p> <blockquote> <p>why isn&#39;t it possible?</p> </blockquote> <p>Unfortunately I have very little knowledge of how native GUI works but I imagine that <a href="/u/driusan" rel="nofollow">/u/driusan</a> (who wrote the editor) will be able answer your questions.</p></pre>driusan: <pre><p>As <a href="/u/neoasterisk" rel="nofollow">/u/neoasterisk</a> pointed out, <a href="https://godoc.org/golang.org/x/exp/shiny" rel="nofollow">shiny</a> is probably as close as you&#39;ll get. It&#39;s part of the Go <code>/x/exp</code> tree, and the question of if <code>/x/</code> will continue to exist notwithstanding, it&#39;s written and maintained by core Go members. Most people want something higher level than just drawing into a window and getting events, but if that&#39;s all you want the <a href="https://github.com/golang/exp/blob/master/shiny/example/basic/main.go" rel="nofollow">basic</a> example should show you enough to get started. That&#39;s pretty much all it does: opens a window, draws into it, and prints the events as they come in.</p> <p>The reason I say &#34;the closest you&#39;ll get&#34; is because for most proprietary operating systems like Windows or Mac, they define their API for interacting with windows in terms of C, and don&#39;t really publish enough of their ABI to do it directly in a language like Go. So your options are: either have a little cgo glue, or compile your glue to asm and hope they don&#39;t change something under the hood without changing the public facing API which breaks your raw assembly syscalls. Open source OSes are less likely to have this problem since there&#39;s either a spec defined for new contributors, or you can just look under the hood if not. If all you care about is X there&#39;s <a href="https://godoc.org/github.com/BurntSushi/xgb" rel="nofollow">xgb</a> written in pure Go as <a href="/u/TheMerovius" rel="nofollow">/u/TheMerovius</a> pointed out.</p></pre>

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

468 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传