Why aren't sync.WaitGroup, sync.Mutex reference types like channel, slices?












-2














If Waitgroups and Mutex always needs to be passed by reference always, can't we make it a reference type (forbid the use of them as pass by value)? I mean is there any use case where we need to use them pass by value?










share|improve this question





























    -2














    If Waitgroups and Mutex always needs to be passed by reference always, can't we make it a reference type (forbid the use of them as pass by value)? I mean is there any use case where we need to use them pass by value?










    share|improve this question



























      -2












      -2








      -2







      If Waitgroups and Mutex always needs to be passed by reference always, can't we make it a reference type (forbid the use of them as pass by value)? I mean is there any use case where we need to use them pass by value?










      share|improve this question















      If Waitgroups and Mutex always needs to be passed by reference always, can't we make it a reference type (forbid the use of them as pass by value)? I mean is there any use case where we need to use them pass by value?







      go concurrency






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 17 '18 at 18:30









      Flimzy

      37.4k96496




      37.4k96496










      asked Nov 17 '18 at 8:27









      Abdul Rahman K

      555416




      555416
























          1 Answer
          1






          active

          oldest

          votes


















          2














          When you pass any argument as value, the value will get copied. Any modification these arguments will be local the the func. When the func exits those changes will be gone.



          In case of a WaitGroup or Mutex you do not want this, as you want to share the state. If all modifications were local, you could not sync anything, as you would have many different copies with different states.



          One valid case could be that you want to copy a WaitGroup or Mutex, but that would be very implicit code and hard for another developer to understand/maintain.






          share|improve this answer























          • I totally agree with what you say. Does it make sense to forbid pass by value of waitgroups and mutex?
            – Abdul Rahman K
            Nov 17 '18 at 8:37










          • Hi. I just extended my answer. Hope that helps.
            – mbuechmann
            Nov 17 '18 at 8:38










          • Still there is this question of when would we want to copy a WaitGroup/ Mutex ?
            – Abdul Rahman K
            Nov 17 '18 at 8:40










          • That depends on your program or what you want to achieve. But I think it is always better to explicitly create a new one.
            – mbuechmann
            Nov 17 '18 at 8:42










          • Exactly the point, It's always better to create a new one. So can we restrict the pass by value ability of them?
            – Abdul Rahman K
            Nov 17 '18 at 8:45











          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%2f53349547%2fwhy-arent-sync-waitgroup-sync-mutex-reference-types-like-channel-slices%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          2














          When you pass any argument as value, the value will get copied. Any modification these arguments will be local the the func. When the func exits those changes will be gone.



          In case of a WaitGroup or Mutex you do not want this, as you want to share the state. If all modifications were local, you could not sync anything, as you would have many different copies with different states.



          One valid case could be that you want to copy a WaitGroup or Mutex, but that would be very implicit code and hard for another developer to understand/maintain.






          share|improve this answer























          • I totally agree with what you say. Does it make sense to forbid pass by value of waitgroups and mutex?
            – Abdul Rahman K
            Nov 17 '18 at 8:37










          • Hi. I just extended my answer. Hope that helps.
            – mbuechmann
            Nov 17 '18 at 8:38










          • Still there is this question of when would we want to copy a WaitGroup/ Mutex ?
            – Abdul Rahman K
            Nov 17 '18 at 8:40










          • That depends on your program or what you want to achieve. But I think it is always better to explicitly create a new one.
            – mbuechmann
            Nov 17 '18 at 8:42










          • Exactly the point, It's always better to create a new one. So can we restrict the pass by value ability of them?
            – Abdul Rahman K
            Nov 17 '18 at 8:45
















          2














          When you pass any argument as value, the value will get copied. Any modification these arguments will be local the the func. When the func exits those changes will be gone.



          In case of a WaitGroup or Mutex you do not want this, as you want to share the state. If all modifications were local, you could not sync anything, as you would have many different copies with different states.



          One valid case could be that you want to copy a WaitGroup or Mutex, but that would be very implicit code and hard for another developer to understand/maintain.






          share|improve this answer























          • I totally agree with what you say. Does it make sense to forbid pass by value of waitgroups and mutex?
            – Abdul Rahman K
            Nov 17 '18 at 8:37










          • Hi. I just extended my answer. Hope that helps.
            – mbuechmann
            Nov 17 '18 at 8:38










          • Still there is this question of when would we want to copy a WaitGroup/ Mutex ?
            – Abdul Rahman K
            Nov 17 '18 at 8:40










          • That depends on your program or what you want to achieve. But I think it is always better to explicitly create a new one.
            – mbuechmann
            Nov 17 '18 at 8:42










          • Exactly the point, It's always better to create a new one. So can we restrict the pass by value ability of them?
            – Abdul Rahman K
            Nov 17 '18 at 8:45














          2












          2








          2






          When you pass any argument as value, the value will get copied. Any modification these arguments will be local the the func. When the func exits those changes will be gone.



          In case of a WaitGroup or Mutex you do not want this, as you want to share the state. If all modifications were local, you could not sync anything, as you would have many different copies with different states.



          One valid case could be that you want to copy a WaitGroup or Mutex, but that would be very implicit code and hard for another developer to understand/maintain.






          share|improve this answer














          When you pass any argument as value, the value will get copied. Any modification these arguments will be local the the func. When the func exits those changes will be gone.



          In case of a WaitGroup or Mutex you do not want this, as you want to share the state. If all modifications were local, you could not sync anything, as you would have many different copies with different states.



          One valid case could be that you want to copy a WaitGroup or Mutex, but that would be very implicit code and hard for another developer to understand/maintain.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 17 '18 at 8:38

























          answered Nov 17 '18 at 8:34









          mbuechmann

          2,80121224




          2,80121224












          • I totally agree with what you say. Does it make sense to forbid pass by value of waitgroups and mutex?
            – Abdul Rahman K
            Nov 17 '18 at 8:37










          • Hi. I just extended my answer. Hope that helps.
            – mbuechmann
            Nov 17 '18 at 8:38










          • Still there is this question of when would we want to copy a WaitGroup/ Mutex ?
            – Abdul Rahman K
            Nov 17 '18 at 8:40










          • That depends on your program or what you want to achieve. But I think it is always better to explicitly create a new one.
            – mbuechmann
            Nov 17 '18 at 8:42










          • Exactly the point, It's always better to create a new one. So can we restrict the pass by value ability of them?
            – Abdul Rahman K
            Nov 17 '18 at 8:45


















          • I totally agree with what you say. Does it make sense to forbid pass by value of waitgroups and mutex?
            – Abdul Rahman K
            Nov 17 '18 at 8:37










          • Hi. I just extended my answer. Hope that helps.
            – mbuechmann
            Nov 17 '18 at 8:38










          • Still there is this question of when would we want to copy a WaitGroup/ Mutex ?
            – Abdul Rahman K
            Nov 17 '18 at 8:40










          • That depends on your program or what you want to achieve. But I think it is always better to explicitly create a new one.
            – mbuechmann
            Nov 17 '18 at 8:42










          • Exactly the point, It's always better to create a new one. So can we restrict the pass by value ability of them?
            – Abdul Rahman K
            Nov 17 '18 at 8:45
















          I totally agree with what you say. Does it make sense to forbid pass by value of waitgroups and mutex?
          – Abdul Rahman K
          Nov 17 '18 at 8:37




          I totally agree with what you say. Does it make sense to forbid pass by value of waitgroups and mutex?
          – Abdul Rahman K
          Nov 17 '18 at 8:37












          Hi. I just extended my answer. Hope that helps.
          – mbuechmann
          Nov 17 '18 at 8:38




          Hi. I just extended my answer. Hope that helps.
          – mbuechmann
          Nov 17 '18 at 8:38












          Still there is this question of when would we want to copy a WaitGroup/ Mutex ?
          – Abdul Rahman K
          Nov 17 '18 at 8:40




          Still there is this question of when would we want to copy a WaitGroup/ Mutex ?
          – Abdul Rahman K
          Nov 17 '18 at 8:40












          That depends on your program or what you want to achieve. But I think it is always better to explicitly create a new one.
          – mbuechmann
          Nov 17 '18 at 8:42




          That depends on your program or what you want to achieve. But I think it is always better to explicitly create a new one.
          – mbuechmann
          Nov 17 '18 at 8:42












          Exactly the point, It's always better to create a new one. So can we restrict the pass by value ability of them?
          – Abdul Rahman K
          Nov 17 '18 at 8:45




          Exactly the point, It's always better to create a new one. So can we restrict the pass by value ability of them?
          – Abdul Rahman K
          Nov 17 '18 at 8:45


















          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.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • 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%2f53349547%2fwhy-arent-sync-waitgroup-sync-mutex-reference-types-like-channel-slices%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

          Biblatex bibliography style without URLs when DOI exists (in Overleaf with Zotero bibliography)

          ComboBox Display Member on multiple fields

          Is it possible to collect Nectar points via Trainline?