Assigning the returning error to an underscore












3















I've been reading some Golang code from github.com/lib/pq which provides drivers for interacting with a postgres database.



Among the code I came across this:



go func() {
select {
case <-done:
_ = cn.cancel()
finished <- struct{}{}
case <-finished:
}
}()


The cancel function looks like:



func (cn *conn) cancel() error


As far as I can tell, the underscore isn't being used as a static assertion about a type (and therefore the compiler doesn't evaluate any side effects as far as I can see (as in this example)) and it isn't a second parameter whereby the author may want to discard it.



In summary: Why assign the result of the cancel function (the error) to an underscore?










share|improve this question


















  • 2





    Apart from being explicit about discarding the error there is no difference between _ = cancel() and cancel().

    – mkopriva
    Dec 30 '18 at 9:56


















3















I've been reading some Golang code from github.com/lib/pq which provides drivers for interacting with a postgres database.



Among the code I came across this:



go func() {
select {
case <-done:
_ = cn.cancel()
finished <- struct{}{}
case <-finished:
}
}()


The cancel function looks like:



func (cn *conn) cancel() error


As far as I can tell, the underscore isn't being used as a static assertion about a type (and therefore the compiler doesn't evaluate any side effects as far as I can see (as in this example)) and it isn't a second parameter whereby the author may want to discard it.



In summary: Why assign the result of the cancel function (the error) to an underscore?










share|improve this question


















  • 2





    Apart from being explicit about discarding the error there is no difference between _ = cancel() and cancel().

    – mkopriva
    Dec 30 '18 at 9:56
















3












3








3


1






I've been reading some Golang code from github.com/lib/pq which provides drivers for interacting with a postgres database.



Among the code I came across this:



go func() {
select {
case <-done:
_ = cn.cancel()
finished <- struct{}{}
case <-finished:
}
}()


The cancel function looks like:



func (cn *conn) cancel() error


As far as I can tell, the underscore isn't being used as a static assertion about a type (and therefore the compiler doesn't evaluate any side effects as far as I can see (as in this example)) and it isn't a second parameter whereby the author may want to discard it.



In summary: Why assign the result of the cancel function (the error) to an underscore?










share|improve this question














I've been reading some Golang code from github.com/lib/pq which provides drivers for interacting with a postgres database.



Among the code I came across this:



go func() {
select {
case <-done:
_ = cn.cancel()
finished <- struct{}{}
case <-finished:
}
}()


The cancel function looks like:



func (cn *conn) cancel() error


As far as I can tell, the underscore isn't being used as a static assertion about a type (and therefore the compiler doesn't evaluate any side effects as far as I can see (as in this example)) and it isn't a second parameter whereby the author may want to discard it.



In summary: Why assign the result of the cancel function (the error) to an underscore?







go error-handling pq






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Dec 30 '18 at 9:12









RambatinoRambatino

1,93711437




1,93711437








  • 2





    Apart from being explicit about discarding the error there is no difference between _ = cancel() and cancel().

    – mkopriva
    Dec 30 '18 at 9:56
















  • 2





    Apart from being explicit about discarding the error there is no difference between _ = cancel() and cancel().

    – mkopriva
    Dec 30 '18 at 9:56










2




2





Apart from being explicit about discarding the error there is no difference between _ = cancel() and cancel().

– mkopriva
Dec 30 '18 at 9:56







Apart from being explicit about discarding the error there is no difference between _ = cancel() and cancel().

– mkopriva
Dec 30 '18 at 9:56














2 Answers
2






active

oldest

votes


















5














Code must be correct. To be sure that code is correct, code must be readable.





The First Rule of Go: Check for errors.





func (cn *conn) cancel() error


If I write



cn.cancel()


did I forget to check for errors or did I decide to discard the error value?



However, if I write



_ = cn.cancel()


I did not forget to check for errors and I did decide to discard the error value.






The Go Programming Language Specification



Blank identifier



The blank identifier is represented by the underscore character _. It
serves as an anonymous placeholder instead of a regular (non-blank)
identifier and has special meaning in declarations, as an operand, and
in assignments.



Assignments



The blank identifier provides a way to ignore right-hand side values
in an assignment:







share|improve this answer

































    3














    The blank identifier “_” is a special anonymous identifier. When used in an assignment, like this case, it provides a way to explicitly ignore right-hand side values. So, the developer has decided to ignore/discard the error returned from this method call.



    A few reasons why they may have done this (based on a quick glance at the method call and context, my guess is 3 or 4):




    1. The method call is guaranteed to succeed in this context.

    2. The error is already handled sufficiently within the method call; no reason to handle it again.

    3. The error doesn’t matter (eg the relevant process is going to end anyway and the outcome will be the same as if the method call has succeeded without error).

    4. The developer was in a hurry to get something working, ignored the error to save time, then failed to come back and handle the error.






    share|improve this answer


























    • Please, can you comment on your downvote so I can attempt to make this better, or learn from my errors?

      – blobdon
      Dec 30 '18 at 11:17











    • Seems very informative to me @blobdon, thanks!

      – Rambatino
      Jan 5 at 7:38











    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%2f53976417%2fassigning-the-returning-error-to-an-underscore%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









    5














    Code must be correct. To be sure that code is correct, code must be readable.





    The First Rule of Go: Check for errors.





    func (cn *conn) cancel() error


    If I write



    cn.cancel()


    did I forget to check for errors or did I decide to discard the error value?



    However, if I write



    _ = cn.cancel()


    I did not forget to check for errors and I did decide to discard the error value.






    The Go Programming Language Specification



    Blank identifier



    The blank identifier is represented by the underscore character _. It
    serves as an anonymous placeholder instead of a regular (non-blank)
    identifier and has special meaning in declarations, as an operand, and
    in assignments.



    Assignments



    The blank identifier provides a way to ignore right-hand side values
    in an assignment:







    share|improve this answer






























      5














      Code must be correct. To be sure that code is correct, code must be readable.





      The First Rule of Go: Check for errors.





      func (cn *conn) cancel() error


      If I write



      cn.cancel()


      did I forget to check for errors or did I decide to discard the error value?



      However, if I write



      _ = cn.cancel()


      I did not forget to check for errors and I did decide to discard the error value.






      The Go Programming Language Specification



      Blank identifier



      The blank identifier is represented by the underscore character _. It
      serves as an anonymous placeholder instead of a regular (non-blank)
      identifier and has special meaning in declarations, as an operand, and
      in assignments.



      Assignments



      The blank identifier provides a way to ignore right-hand side values
      in an assignment:







      share|improve this answer




























        5












        5








        5







        Code must be correct. To be sure that code is correct, code must be readable.





        The First Rule of Go: Check for errors.





        func (cn *conn) cancel() error


        If I write



        cn.cancel()


        did I forget to check for errors or did I decide to discard the error value?



        However, if I write



        _ = cn.cancel()


        I did not forget to check for errors and I did decide to discard the error value.






        The Go Programming Language Specification



        Blank identifier



        The blank identifier is represented by the underscore character _. It
        serves as an anonymous placeholder instead of a regular (non-blank)
        identifier and has special meaning in declarations, as an operand, and
        in assignments.



        Assignments



        The blank identifier provides a way to ignore right-hand side values
        in an assignment:







        share|improve this answer















        Code must be correct. To be sure that code is correct, code must be readable.





        The First Rule of Go: Check for errors.





        func (cn *conn) cancel() error


        If I write



        cn.cancel()


        did I forget to check for errors or did I decide to discard the error value?



        However, if I write



        _ = cn.cancel()


        I did not forget to check for errors and I did decide to discard the error value.






        The Go Programming Language Specification



        Blank identifier



        The blank identifier is represented by the underscore character _. It
        serves as an anonymous placeholder instead of a regular (non-blank)
        identifier and has special meaning in declarations, as an operand, and
        in assignments.



        Assignments



        The blank identifier provides a way to ignore right-hand side values
        in an assignment:








        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Dec 30 '18 at 12:50

























        answered Dec 30 '18 at 11:07









        peterSOpeterSO

        93.9k14160175




        93.9k14160175

























            3














            The blank identifier “_” is a special anonymous identifier. When used in an assignment, like this case, it provides a way to explicitly ignore right-hand side values. So, the developer has decided to ignore/discard the error returned from this method call.



            A few reasons why they may have done this (based on a quick glance at the method call and context, my guess is 3 or 4):




            1. The method call is guaranteed to succeed in this context.

            2. The error is already handled sufficiently within the method call; no reason to handle it again.

            3. The error doesn’t matter (eg the relevant process is going to end anyway and the outcome will be the same as if the method call has succeeded without error).

            4. The developer was in a hurry to get something working, ignored the error to save time, then failed to come back and handle the error.






            share|improve this answer


























            • Please, can you comment on your downvote so I can attempt to make this better, or learn from my errors?

              – blobdon
              Dec 30 '18 at 11:17











            • Seems very informative to me @blobdon, thanks!

              – Rambatino
              Jan 5 at 7:38
















            3














            The blank identifier “_” is a special anonymous identifier. When used in an assignment, like this case, it provides a way to explicitly ignore right-hand side values. So, the developer has decided to ignore/discard the error returned from this method call.



            A few reasons why they may have done this (based on a quick glance at the method call and context, my guess is 3 or 4):




            1. The method call is guaranteed to succeed in this context.

            2. The error is already handled sufficiently within the method call; no reason to handle it again.

            3. The error doesn’t matter (eg the relevant process is going to end anyway and the outcome will be the same as if the method call has succeeded without error).

            4. The developer was in a hurry to get something working, ignored the error to save time, then failed to come back and handle the error.






            share|improve this answer


























            • Please, can you comment on your downvote so I can attempt to make this better, or learn from my errors?

              – blobdon
              Dec 30 '18 at 11:17











            • Seems very informative to me @blobdon, thanks!

              – Rambatino
              Jan 5 at 7:38














            3












            3








            3







            The blank identifier “_” is a special anonymous identifier. When used in an assignment, like this case, it provides a way to explicitly ignore right-hand side values. So, the developer has decided to ignore/discard the error returned from this method call.



            A few reasons why they may have done this (based on a quick glance at the method call and context, my guess is 3 or 4):




            1. The method call is guaranteed to succeed in this context.

            2. The error is already handled sufficiently within the method call; no reason to handle it again.

            3. The error doesn’t matter (eg the relevant process is going to end anyway and the outcome will be the same as if the method call has succeeded without error).

            4. The developer was in a hurry to get something working, ignored the error to save time, then failed to come back and handle the error.






            share|improve this answer















            The blank identifier “_” is a special anonymous identifier. When used in an assignment, like this case, it provides a way to explicitly ignore right-hand side values. So, the developer has decided to ignore/discard the error returned from this method call.



            A few reasons why they may have done this (based on a quick glance at the method call and context, my guess is 3 or 4):




            1. The method call is guaranteed to succeed in this context.

            2. The error is already handled sufficiently within the method call; no reason to handle it again.

            3. The error doesn’t matter (eg the relevant process is going to end anyway and the outcome will be the same as if the method call has succeeded without error).

            4. The developer was in a hurry to get something working, ignored the error to save time, then failed to come back and handle the error.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Dec 30 '18 at 11:40

























            answered Dec 30 '18 at 9:55









            blobdonblobdon

            57237




            57237













            • Please, can you comment on your downvote so I can attempt to make this better, or learn from my errors?

              – blobdon
              Dec 30 '18 at 11:17











            • Seems very informative to me @blobdon, thanks!

              – Rambatino
              Jan 5 at 7:38



















            • Please, can you comment on your downvote so I can attempt to make this better, or learn from my errors?

              – blobdon
              Dec 30 '18 at 11:17











            • Seems very informative to me @blobdon, thanks!

              – Rambatino
              Jan 5 at 7:38

















            Please, can you comment on your downvote so I can attempt to make this better, or learn from my errors?

            – blobdon
            Dec 30 '18 at 11:17





            Please, can you comment on your downvote so I can attempt to make this better, or learn from my errors?

            – blobdon
            Dec 30 '18 at 11:17













            Seems very informative to me @blobdon, thanks!

            – Rambatino
            Jan 5 at 7:38





            Seems very informative to me @blobdon, thanks!

            – Rambatino
            Jan 5 at 7:38


















            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%2f53976417%2fassigning-the-returning-error-to-an-underscore%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

            Monofisismo

            Angular Downloading a file using contenturl with Basic Authentication

            Olmecas