30 行 Go 能做什么?您能创建一个有用的、完整的程序来展示其功能吗?
因此,过去几天的热门话题是 Google 的新语言 Go。假设你们都是像我一样痴迷的编程语言极客,你们都下载了它,构建了它,并运行了您的“Hello,世界”程序(使用 UTF-8 发明者编写的语言不是很好吗?) 。你们都已阅读教程,Effective Go,以及其他一些文档。
现在,你打算用它做什么?
我想看一些展示 Go 强大功能的演示。在一个简短的程序中你能做什么?展示您最好的示例代码。虽然只有在需求不断变化的项目过程中与一个由许多程序员组成的团队一起编写和维护一个大型代码库,才能真正衡量一门语言的真正程度,看看你能在有限的时间内完成多少工作代码确实有助于展示语言的表达能力。我希望看到简短而完整的程序,能够真正发挥 Go 独特的新功能;不仅仅是片段或“你好,世界”。
因此,发布一些您用 Go 编写的很酷的代码。利用其独特的功能,例如它的 goroutine 和并发通道,或其基于接口的类型系统。你能编写一个原始的聊天服务器,或者很酷的 IRC 机器人吗?实现可扩展到多个核心的并行 Mandelbrot 集?为某种小语言编写一个解释器?您能用 30 行代码完成所有工作吗?
我任意选择了 30 个,尽可能多地适合 Stack Overflow 代码块,而不会溢出并出现滚动条;它应该足以做一些有趣的事情而无需打太多高尔夫球,但又足够短以吸引每个人的注意力以进行快速演示。例如,只需进行一些重新格式化,示例 Web 服务器 就应该能够适合(不计算数据)。
向我们展示您的 Go 代码!
So, the big buzz in the last few days is Go, the new language from Google. Assuming you're all obsessive programming language geeks like me, you've all downloaded it, built it, and run your "Hello, 世界" program (isn't it nice using a language written by the inventors of UTF-8?). You've all read the tutorial, Effective Go, and some of the other docs.
Now, what are you going to do with it?
I'd like to see some demos that show off the power of Go. What can you do in a brief program? Show off your best example code. While the true measure of a language can't really be taken until you've written and maintained a large codebase with a team of many programmers over the course of a project with changing requirements, seeing how much you can do in a limited amount of code does help to demonstrate the expressive power of a language. I'd like to see short, complete programs that truly exercise the unique new features of Go; not just snippets or "Hello, World".
So, post some cool code you've written with Go. Take advantage of its unique features, like its goroutines and channels for concurrency, or its interface based type system. Can you write a primitive chat server, or cool IRC bot? Implement a parallel Mandelbrot set that scales to many cores? Write an interpreter for some tiny language? And can you do it all in 30 lines?
I chose 30 arbitrarily as about as much as you can fit into a Stack Overflow code block without it overflowing and getting a scroll bar; it should be enough to do something interesting without golfing too much, but short enough to keep everyone's attention for a quick demo. For instance, with just a bit of reformatting, the example web server should be able to fit (not counting the data).
Show us your Go code!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这是我编写的一个 Web 代理,用于提供对需要 HTTP 基本身份验证的 Web 服务的未经身份验证的访问。我需要它作为内部的东西(并且仍在使用它):
This is a web proxy I wrote to provide unauthenticated access to a web service that required HTTP basic auth. I needed it for an internal thingy (and still use it):
这使得钟面的 PNG(在标准输出上)显示当前时间。它勉强能容纳三十行,因此代码并不像应有的那么干净。
像这样运行它
注意到所有这些 float64 转换了吗?我从未见过像 Go 这样严格类型的语言。
这是使用
go fix
修复的相同代码(以及一些手动调整),然后使用go fmt
自动格式化。一些换行符是手动插入的。This makes a PNG (on stdout) of a clock face showing the current time. It's barely golfed to fit thirty lines, so the code is not quite as clean as it should be.
Run it like
Notice all those float64 casts? I've NEVER seen a language as strict as Go about types.
This is the same code fixed with
go fix
(and some manual tweaking) and then automatically formatted usinggo fmt
. Some newlines where inserted manually.好的,我会开始行动。这是我的第一个 Go 程序。这是一个非常原始的聊天服务器,如果我把它压缩一下,就可以容纳 30 行,每行 80 个字符;格式为
gofmt
,共 60 行。它侦听硬编码端口(4242),基本上不进行错误处理,并且除了在收到错误时停止尝试从客户端读取之外不处理客户端断开连接。构建并运行:
然后在其他几个终端中,连接
OK, I'll get the ball rolling. Here's my first Go program. It's a very primitive chat server, and fits in 30 lines of 80 characters if I compress it down a bit; formatted with
gofmt
, it is 60 lines. It listens on a hard coded port (4242), does basically no error handling, and doesn't handle client disconnection other than stopping trying to read from a client if it gets an error.Build and run with:
And then in a few other terminals, connect with
我真的很喜欢 go 的通道和
select
语句,所以这里展示了表达“在特定时间内去获取尽可能多的东西”的概念是多么容易。这会在 300 毫秒内生成尽可能多的随机数,并返回该时间内生成的最大随机数。
I really like go's channels and the
select
statement, so here's something that shows how easy it is to express the "go and get as many things as possible within a certain time" concept.This generates as many random numbers as possible within 300 milliseconds and returns the biggest one generated in that time.
我从某处复制了这个。相当简单,但显示了一些功能。
I copied this from somewhere. Fairly simple, but shows some features.