Is it possible to use Go's buffered channel as a thread-safe queue?

channel, go, queue

Solution

I'm pretty sure that Channels are FIFO. They are also cheap so they would be memory efficient. Beyond that without knowing the details of how you are going to use them We can't really give much more advice.

Problem

I want to find a queue structure (a data container) whose elements must be first-in-first-out. It is important for me that the structure must be thread-safe. I'm going to use this data container as something like a task or connection pool. I know a buffered channel is thread-safe, but I wonder if it works as FIFO, especially in a concurrent situation. And if it is possible to use buffered channel as a thread-safe queue, do I need to worry about its efficiency?

Original source