英文:
What are the options for network programming in Go?
问题
我想要编写一个简单的客户端/服务器程序,使用TCP套接字。有没有关于如何在Go中进行网络编程的想法?
英文:
I am looking to write a simple client/server utilizing TCP sockets. Any ideas how to do network programming in Go?
答案1
得分: 5
Go对网络编程有很好的支持。
对于服务器来说,最简单的方法是让main()
函数启动一个TCP接受循环,并为每个请求生成一个goroutine来处理。
我写的第一个Go软件是一个memcached服务器。你可以参考gomemcached来了解如何开始编写服务器。
客户端不应该特别困难。在许多情况下,最合理的做法可能是使用共享的客户端,并使用goroutine进行入站通信,就像我在gomemcached中与实际存储层通信一样。
当然,这不是唯一的方法。也许在实验中你会找到更好的解决方案。
英文:
Go has fine support for networking.
For a server, the easiest thing to do is have your main()
start a tcp accept loop and spawn a goroutine to handle each request.
The first go software I wrote was a memcached server. You may want to check out gomemcached for an idea of how to get started on servers.
Clients shouldn't be particularly harder. In many cases, it may make the most sense to have a shared client with goroutines for inbound communication much like I use in gomemcached for communicating with the actual storage layer.
Of course, this isn't the only way. Perhaps you'll find something better as you experiment.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论