TCP是在同一主机中Go程序之间进行通信的最佳方法吗?

Suppose I have two separated Go programs running in my localhost, is TCP the best method for transferring data between the two programs in terms of performance?

It would work and give additional freedom to have the two programs run on different computers. But it is not the best in terms of performance.

For good performance, shared memory comes to mind. https://en.wikipedia.org/wiki/Shared_memory Maybe you could describe a bit more what exactly you want to do.

The short answer is no. The TCP/IP stack is slow, especially the TCP part. So in terms of performance you better use local inter-process communication methods, like a shared memory between your applications or Unix sockets.

If you MUST use a network stack to communicate (say, you plan to move applications between hosts), then UDP or raw sockets are the best options in terms of performance.

And only if you:

  1. must use a network and
  2. you need a reliable communication channel, then TCP is a good option.

So just walk through your requirements and decide if it is a best method for you.