Go语言中的网络编程选项有哪些?

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

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.

huangapple
  • 本文由 发表于 2009年12月12日 00:40:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/1889257.html
匿名

发表评论

匿名网友

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

确定