How to write nice and clean Go package?

Multi tool use
Multi tool use









1















I try to implement Sparse sets from this article in Go and make it into a package. In early implementation, the API is clean and minimal with only exported type Sparse and exported method Insert, Delete, Has, Union, Intersection, Clear, and Len, pretty much only basic sets operation.



Later on, I want to add new functionality, sets that can reserve an element automatically (lets call it AutoSparse). If Sparse have Insert(k int) which insert k into sets, AutoSparse have Reserved() reserved an available element. If I have 0 1 2 4 5 in AutoSparse, when i call Reserve() it must add 3, not 6 so now it become 0 1 2 4 5 3. Here's the implementation at playground.



As you can see, in order to maintain which element to added into sets, it doesn't add new field in the struct and I want to keep it like that.



My Question is how to add that new functionality to my package without adding new exported type AutoSparse to keep the API clean and minimal?



This is what i already try:



  1. I can use interface to hide implementation but function signature is different, one use Insert(k int), the other use Reserve(), even if I use name Insert() it still different, or should I use Insert(k int) but didn't use k at all? it can but it's awkward.

  2. I can't use the same struct to implement this because once you Use Reserve() to add element, you can't use Insert(k int) because it will messed up the reserved element, even Delete and Clear is different.









share|improve this question




























    1















    I try to implement Sparse sets from this article in Go and make it into a package. In early implementation, the API is clean and minimal with only exported type Sparse and exported method Insert, Delete, Has, Union, Intersection, Clear, and Len, pretty much only basic sets operation.



    Later on, I want to add new functionality, sets that can reserve an element automatically (lets call it AutoSparse). If Sparse have Insert(k int) which insert k into sets, AutoSparse have Reserved() reserved an available element. If I have 0 1 2 4 5 in AutoSparse, when i call Reserve() it must add 3, not 6 so now it become 0 1 2 4 5 3. Here's the implementation at playground.



    As you can see, in order to maintain which element to added into sets, it doesn't add new field in the struct and I want to keep it like that.



    My Question is how to add that new functionality to my package without adding new exported type AutoSparse to keep the API clean and minimal?



    This is what i already try:



    1. I can use interface to hide implementation but function signature is different, one use Insert(k int), the other use Reserve(), even if I use name Insert() it still different, or should I use Insert(k int) but didn't use k at all? it can but it's awkward.

    2. I can't use the same struct to implement this because once you Use Reserve() to add element, you can't use Insert(k int) because it will messed up the reserved element, even Delete and Clear is different.









    share|improve this question


























      1












      1








      1


      0






      I try to implement Sparse sets from this article in Go and make it into a package. In early implementation, the API is clean and minimal with only exported type Sparse and exported method Insert, Delete, Has, Union, Intersection, Clear, and Len, pretty much only basic sets operation.



      Later on, I want to add new functionality, sets that can reserve an element automatically (lets call it AutoSparse). If Sparse have Insert(k int) which insert k into sets, AutoSparse have Reserved() reserved an available element. If I have 0 1 2 4 5 in AutoSparse, when i call Reserve() it must add 3, not 6 so now it become 0 1 2 4 5 3. Here's the implementation at playground.



      As you can see, in order to maintain which element to added into sets, it doesn't add new field in the struct and I want to keep it like that.



      My Question is how to add that new functionality to my package without adding new exported type AutoSparse to keep the API clean and minimal?



      This is what i already try:



      1. I can use interface to hide implementation but function signature is different, one use Insert(k int), the other use Reserve(), even if I use name Insert() it still different, or should I use Insert(k int) but didn't use k at all? it can but it's awkward.

      2. I can't use the same struct to implement this because once you Use Reserve() to add element, you can't use Insert(k int) because it will messed up the reserved element, even Delete and Clear is different.









      share|improve this question
















      I try to implement Sparse sets from this article in Go and make it into a package. In early implementation, the API is clean and minimal with only exported type Sparse and exported method Insert, Delete, Has, Union, Intersection, Clear, and Len, pretty much only basic sets operation.



      Later on, I want to add new functionality, sets that can reserve an element automatically (lets call it AutoSparse). If Sparse have Insert(k int) which insert k into sets, AutoSparse have Reserved() reserved an available element. If I have 0 1 2 4 5 in AutoSparse, when i call Reserve() it must add 3, not 6 so now it become 0 1 2 4 5 3. Here's the implementation at playground.



      As you can see, in order to maintain which element to added into sets, it doesn't add new field in the struct and I want to keep it like that.



      My Question is how to add that new functionality to my package without adding new exported type AutoSparse to keep the API clean and minimal?



      This is what i already try:



      1. I can use interface to hide implementation but function signature is different, one use Insert(k int), the other use Reserve(), even if I use name Insert() it still different, or should I use Insert(k int) but didn't use k at all? it can but it's awkward.

      2. I can't use the same struct to implement this because once you Use Reserve() to add element, you can't use Insert(k int) because it will messed up the reserved element, even Delete and Clear is different.






      go package






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 13 '18 at 13:31









      Flimzy

      38.5k106597




      38.5k106597










      asked Nov 13 '18 at 11:48









      billyzaelanibillyzaelani

      10912




      10912






















          1 Answer
          1






          active

          oldest

          votes


















          2














          You can use k... int as parameter.



          func (sparse Sparse) Insert(k... int) error 
          if len(k) == 0
          // first case
          return nil
          else if len(k) == 1
          // second case
          return nil
          else
          return errors.New("Too many arguments")







          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%2f53280394%2fhow-to-write-nice-and-clean-go-package%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














            You can use k... int as parameter.



            func (sparse Sparse) Insert(k... int) error 
            if len(k) == 0
            // first case
            return nil
            else if len(k) == 1
            // second case
            return nil
            else
            return errors.New("Too many arguments")







            share|improve this answer



























              2














              You can use k... int as parameter.



              func (sparse Sparse) Insert(k... int) error 
              if len(k) == 0
              // first case
              return nil
              else if len(k) == 1
              // second case
              return nil
              else
              return errors.New("Too many arguments")







              share|improve this answer

























                2












                2








                2







                You can use k... int as parameter.



                func (sparse Sparse) Insert(k... int) error 
                if len(k) == 0
                // first case
                return nil
                else if len(k) == 1
                // second case
                return nil
                else
                return errors.New("Too many arguments")







                share|improve this answer













                You can use k... int as parameter.



                func (sparse Sparse) Insert(k... int) error 
                if len(k) == 0
                // first case
                return nil
                else if len(k) == 1
                // second case
                return nil
                else
                return errors.New("Too many arguments")








                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 13 '18 at 14:52









                Maxim PanfilovMaxim Panfilov

                160111




                160111





























                    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%2f53280394%2fhow-to-write-nice-and-clean-go-package%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







                    F0Zodt1x zVGU,F28FSyKDKzKtVD,ixDCBm,aD0Kje kDOlMLsPTj HT1wxTjCw52V3xM
                    WKTJldwU0Cn1tyYFc1,NLUSSv W,wM0Ws8fPYl MuxaJH DJfs2Ys41 V25MFtdyvzC48

                    Popular posts from this blog

                    Use pre created SQLite database for Android project in kotlin

                    Ruanda

                    Ondo