The differences between channel buffer capacity of zero and one in golang





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







1















I have set channel buffer size to zero, like var intChannelZero = make(chan int) , when getting value from the intChannelZero will be blocked until the intChannelZero has value.



Also, I set channel buffer size to one, like var intChannelOne = make(chan int, 1), when getting value from the intChannelOne will be blocked until the intChannelOne has value.



We know the capacity of intChannelZero is zero, the capacity of intChannelOne is one, so I want to know:




  • When putting a value to the intChannelZero like intChannelZero <- 1, where the value be saved?

  • The differences between intChannelZero and intChannelOne when putting a value to them.


Who can explain it at the level of Golang Runtime Enviroment? Thanks a lot.










share|improve this question































    1















    I have set channel buffer size to zero, like var intChannelZero = make(chan int) , when getting value from the intChannelZero will be blocked until the intChannelZero has value.



    Also, I set channel buffer size to one, like var intChannelOne = make(chan int, 1), when getting value from the intChannelOne will be blocked until the intChannelOne has value.



    We know the capacity of intChannelZero is zero, the capacity of intChannelOne is one, so I want to know:




    • When putting a value to the intChannelZero like intChannelZero <- 1, where the value be saved?

    • The differences between intChannelZero and intChannelOne when putting a value to them.


    Who can explain it at the level of Golang Runtime Enviroment? Thanks a lot.










    share|improve this question



























      1












      1








      1


      1






      I have set channel buffer size to zero, like var intChannelZero = make(chan int) , when getting value from the intChannelZero will be blocked until the intChannelZero has value.



      Also, I set channel buffer size to one, like var intChannelOne = make(chan int, 1), when getting value from the intChannelOne will be blocked until the intChannelOne has value.



      We know the capacity of intChannelZero is zero, the capacity of intChannelOne is one, so I want to know:




      • When putting a value to the intChannelZero like intChannelZero <- 1, where the value be saved?

      • The differences between intChannelZero and intChannelOne when putting a value to them.


      Who can explain it at the level of Golang Runtime Enviroment? Thanks a lot.










      share|improve this question
















      I have set channel buffer size to zero, like var intChannelZero = make(chan int) , when getting value from the intChannelZero will be blocked until the intChannelZero has value.



      Also, I set channel buffer size to one, like var intChannelOne = make(chan int, 1), when getting value from the intChannelOne will be blocked until the intChannelOne has value.



      We know the capacity of intChannelZero is zero, the capacity of intChannelOne is one, so I want to know:




      • When putting a value to the intChannelZero like intChannelZero <- 1, where the value be saved?

      • The differences between intChannelZero and intChannelOne when putting a value to them.


      Who can explain it at the level of Golang Runtime Enviroment? Thanks a lot.







      go






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 17 '18 at 4:34







      Shuai Junlan

















      asked Nov 17 '18 at 4:25









      Shuai JunlanShuai Junlan

      65110




      65110
























          2 Answers
          2






          active

          oldest

          votes


















          4














          If the channel is unbuffered (capacity is zero), then communication succeeds only when the sender and receiver are both ready.



          If the channel is buffered (capacity >= 1), then send succeeds without blocking if the channel is not full and receive succeeds without blocking if the buffer is not empty.




          When putting a value to the intChannelZero like intChannelZero <- 1, where the value be saved?




          The value is copied from the sender to the receiver. The value is not saved anywhere other than whatever temporary variables the implementation might use.




          The differences between intChannelZero and intChannelOne when putting a value to them.




          Send on intChannelZero blocks until a receiver is ready.



          Send on intChannelOne blocks until space is available in the buffer.






          share|improve this answer































            0














            Both unbuffered and buffered channel differences will be in terms of,




            1. Sends to channel is blocked

            2. Receives from channel is blocked


            For unbuffered channels



            Sends will be blocked if the channel already send a message and hasn't yet been received.



            Receives will be blocked if no sends ever happened.



            For buffered channels



            Sends will be blocked if already n(channel size) sends happened and none of them received. ie entire channel size has been used by messages send but nothing was received.



            Receives will be blocked if the buffer is empty ie doesn't have any unconsumed sends



            Runtime errors



            Receive blocked will throw the bellow error



            fatal error: all goroutines are asleep - deadlock!

            goroutine 1 [chan receive]:


            Send blocked will throw the bellow error



            fatal error: all goroutines are asleep - deadlock!

            goroutine 1 [chan send]:





            share|improve this answer
























              Your Answer






              StackExchange.ifUsing("editor", function () {
              StackExchange.using("externalEditor", function () {
              StackExchange.using("snippets", function () {
              StackExchange.snippets.init();
              });
              });
              }, "code-snippets");

              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "1"
              };
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function() {
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled) {
              StackExchange.using("snippets", function() {
              createEditor();
              });
              }
              else {
              createEditor();
              }
              });

              function createEditor() {
              StackExchange.prepareEditor({
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader: {
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              },
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              });


              }
              });














              draft saved

              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53348202%2fthe-differences-between-channel-buffer-capacity-of-zero-and-one-in-golang%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              4














              If the channel is unbuffered (capacity is zero), then communication succeeds only when the sender and receiver are both ready.



              If the channel is buffered (capacity >= 1), then send succeeds without blocking if the channel is not full and receive succeeds without blocking if the buffer is not empty.




              When putting a value to the intChannelZero like intChannelZero <- 1, where the value be saved?




              The value is copied from the sender to the receiver. The value is not saved anywhere other than whatever temporary variables the implementation might use.




              The differences between intChannelZero and intChannelOne when putting a value to them.




              Send on intChannelZero blocks until a receiver is ready.



              Send on intChannelOne blocks until space is available in the buffer.






              share|improve this answer




























                4














                If the channel is unbuffered (capacity is zero), then communication succeeds only when the sender and receiver are both ready.



                If the channel is buffered (capacity >= 1), then send succeeds without blocking if the channel is not full and receive succeeds without blocking if the buffer is not empty.




                When putting a value to the intChannelZero like intChannelZero <- 1, where the value be saved?




                The value is copied from the sender to the receiver. The value is not saved anywhere other than whatever temporary variables the implementation might use.




                The differences between intChannelZero and intChannelOne when putting a value to them.




                Send on intChannelZero blocks until a receiver is ready.



                Send on intChannelOne blocks until space is available in the buffer.






                share|improve this answer


























                  4












                  4








                  4







                  If the channel is unbuffered (capacity is zero), then communication succeeds only when the sender and receiver are both ready.



                  If the channel is buffered (capacity >= 1), then send succeeds without blocking if the channel is not full and receive succeeds without blocking if the buffer is not empty.




                  When putting a value to the intChannelZero like intChannelZero <- 1, where the value be saved?




                  The value is copied from the sender to the receiver. The value is not saved anywhere other than whatever temporary variables the implementation might use.




                  The differences between intChannelZero and intChannelOne when putting a value to them.




                  Send on intChannelZero blocks until a receiver is ready.



                  Send on intChannelOne blocks until space is available in the buffer.






                  share|improve this answer













                  If the channel is unbuffered (capacity is zero), then communication succeeds only when the sender and receiver are both ready.



                  If the channel is buffered (capacity >= 1), then send succeeds without blocking if the channel is not full and receive succeeds without blocking if the buffer is not empty.




                  When putting a value to the intChannelZero like intChannelZero <- 1, where the value be saved?




                  The value is copied from the sender to the receiver. The value is not saved anywhere other than whatever temporary variables the implementation might use.




                  The differences between intChannelZero and intChannelOne when putting a value to them.




                  Send on intChannelZero blocks until a receiver is ready.



                  Send on intChannelOne blocks until space is available in the buffer.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 17 '18 at 6:27









                  Cerise LimónCerise Limón

                  55.7k57395




                  55.7k57395

























                      0














                      Both unbuffered and buffered channel differences will be in terms of,




                      1. Sends to channel is blocked

                      2. Receives from channel is blocked


                      For unbuffered channels



                      Sends will be blocked if the channel already send a message and hasn't yet been received.



                      Receives will be blocked if no sends ever happened.



                      For buffered channels



                      Sends will be blocked if already n(channel size) sends happened and none of them received. ie entire channel size has been used by messages send but nothing was received.



                      Receives will be blocked if the buffer is empty ie doesn't have any unconsumed sends



                      Runtime errors



                      Receive blocked will throw the bellow error



                      fatal error: all goroutines are asleep - deadlock!

                      goroutine 1 [chan receive]:


                      Send blocked will throw the bellow error



                      fatal error: all goroutines are asleep - deadlock!

                      goroutine 1 [chan send]:





                      share|improve this answer




























                        0














                        Both unbuffered and buffered channel differences will be in terms of,




                        1. Sends to channel is blocked

                        2. Receives from channel is blocked


                        For unbuffered channels



                        Sends will be blocked if the channel already send a message and hasn't yet been received.



                        Receives will be blocked if no sends ever happened.



                        For buffered channels



                        Sends will be blocked if already n(channel size) sends happened and none of them received. ie entire channel size has been used by messages send but nothing was received.



                        Receives will be blocked if the buffer is empty ie doesn't have any unconsumed sends



                        Runtime errors



                        Receive blocked will throw the bellow error



                        fatal error: all goroutines are asleep - deadlock!

                        goroutine 1 [chan receive]:


                        Send blocked will throw the bellow error



                        fatal error: all goroutines are asleep - deadlock!

                        goroutine 1 [chan send]:





                        share|improve this answer


























                          0












                          0








                          0







                          Both unbuffered and buffered channel differences will be in terms of,




                          1. Sends to channel is blocked

                          2. Receives from channel is blocked


                          For unbuffered channels



                          Sends will be blocked if the channel already send a message and hasn't yet been received.



                          Receives will be blocked if no sends ever happened.



                          For buffered channels



                          Sends will be blocked if already n(channel size) sends happened and none of them received. ie entire channel size has been used by messages send but nothing was received.



                          Receives will be blocked if the buffer is empty ie doesn't have any unconsumed sends



                          Runtime errors



                          Receive blocked will throw the bellow error



                          fatal error: all goroutines are asleep - deadlock!

                          goroutine 1 [chan receive]:


                          Send blocked will throw the bellow error



                          fatal error: all goroutines are asleep - deadlock!

                          goroutine 1 [chan send]:





                          share|improve this answer













                          Both unbuffered and buffered channel differences will be in terms of,




                          1. Sends to channel is blocked

                          2. Receives from channel is blocked


                          For unbuffered channels



                          Sends will be blocked if the channel already send a message and hasn't yet been received.



                          Receives will be blocked if no sends ever happened.



                          For buffered channels



                          Sends will be blocked if already n(channel size) sends happened and none of them received. ie entire channel size has been used by messages send but nothing was received.



                          Receives will be blocked if the buffer is empty ie doesn't have any unconsumed sends



                          Runtime errors



                          Receive blocked will throw the bellow error



                          fatal error: all goroutines are asleep - deadlock!

                          goroutine 1 [chan receive]:


                          Send blocked will throw the bellow error



                          fatal error: all goroutines are asleep - deadlock!

                          goroutine 1 [chan send]:






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 17 '18 at 7:16









                          JeevanJeevan

                          1426




                          1426






























                              draft saved

                              draft discarded




















































                              Thanks for contributing an answer to Stack Overflow!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53348202%2fthe-differences-between-channel-buffer-capacity-of-zero-and-one-in-golang%23new-answer', 'question_page');
                              }
                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              List item for chat from Array inside array React Native

                              Thiostrepton

                              Caerphilly