The Wiert Corner – irregular stream of stuff

Jeroen W. Pluimers on .NET, C#, Delphi, databases, and personal interests

  • My badges

  • Twitter Updates

  • My Flickr Stream

  • Pages

  • All categories

  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 2,119 other followers

variables – What is “_,” in a Golang declaration? – Stack Overflow

Posted by jpluimers on 2020/09/29

As a (then) go lang n00b, the less upvoted answers helped me e lot: [WayBack] variables – What is “_,” in a Golang declaration? – Stack Overflow:

  • The Go compiler won’t allow you to create variables that you never use.

    for i, value := range x {
       total += value
    }

    The above code will return an error message “i declared and not used”.

    Since we don’t use i inside of our loop we need to change it to this:

    for _, value := range x {
       total += value
    }
  • _ is the blank identifier. Meaning the value it should be assigned is discarded.

    Here it is the value of example key that is discarded. The second line of code would discard the presence boolean and store the value in prs.
    So to only check the presence in the map, you can discard the value. This can be used to use a map as a set.

–jeroen

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

 
%d bloggers like this: