Underscore symbol

127 views
Skip to first unread message

Canuto

unread,
Nov 4, 2022, 1:54:34 PM11/4/22
to golang-nuts
Hello everybody,
I'm just starting out with go ...
I have searched for lights on this string but without success.
What does this sign mean " _, err " , what the underscore symbol means here ?                      
               
 func generateSalt() string { 
 randomBytes := make([]byte, 16) 
 _, err := rand.Read(randomBytes)
  if err != nil {                          
return "" }
                    

Regards.
           
       
      
      

Jan Mercl

unread,
Nov 4, 2022, 2:02:26 PM11/4/22
to Canuto, golang-nuts
On Fri, Nov 4, 2022 at 6:54 PM Canuto <testva...@gmail.com> wrote:

> I'm just starting out with go ...
> I have searched for lights on this string but without success.
> What does this sign mean " _, err " , what the underscore symbol means here ?
>
> func generateSalt() string {
> randomBytes := make([]byte, 16)
> _, err := rand.Read(randomBytes)
> if err != nil {
> return "" }

The language specification is the best place where to look for such
information: https://go.dev/ref/spec#Blank_identifier

Francesco Bottoni

unread,
Nov 4, 2022, 2:47:21 PM11/4/22
to Jan Mercl, Canuto, golang-nuts
Means: "just ignore the result at the _ position"





--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/CAA40n-WgGJ4uYQ1qG-JDATG06PTWu3D%3DEf-t-PtH_QGOnEr0kQ%40mail.gmail.com.

Konstantin Khomoutov

unread,
Nov 4, 2022, 5:10:20 PM11/4/22
to golang-nuts
On Fri, Nov 04, 2022 at 04:58:35AM -0700, Canuto wrote:

> I'm just starting out with go ...
> I have searched for lights on this string but without success.
> What does this sign mean " _, err " , what the underscore symbol means here?

If you're starting with Go, please start with the Go Tour [1].
For instance, the use of this underscore sign is covered in [2].

1. https://go.dev/tour/
2. https://go.dev/tour/moretypes/17

Marcel Huijkman

unread,
Nov 5, 2022, 4:19:03 AM11/5/22
to golang-nuts
When I explain it during my training I always say it is a trashcan variable, anything you put in is to be ignored on the spot.

Chris Burkert

unread,
Nov 5, 2022, 4:57:50 AM11/5/22
to Marcel Huijkman, golang-nuts
I am curious: from a compiler perspective, does that mean that by using _ simply less assembler instructions are created by not handling those registers which relate to _?

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.

David Finkel

unread,
Nov 5, 2022, 10:38:55 PM11/5/22
to Chris Burkert, Marcel Huijkman, golang-nuts
On Sat, Nov 5, 2022 at 4:57 AM Chris Burkert <burker...@gmail.com> wrote:
I am curious: from a compiler perspective, does that mean that by using _ simply less assembler instructions are created by not handling those registers which relate to _?

 
That kind of depends on what you mean by "not handling". Since it's a compile-time error (in the GC compiler) to have an unused variable, you'd have to do something else with that value.
Depending on what type that ignored return value is, whether the call is inlined and whether the call is using the newer, register-based calling convention the compiler may be able to prune instructions related to that variable.

Here are a few scenarios off the top of my head (in no particular order and by no means exhaustive, but some of them are interesting -- and some are completely hypothetical optimizations as I haven't looked at a lot of the relevant compiler code myself):
  • The call doesn't get inlined, and it's a trivial value that can be passed around in a register, so that return register is simply never read (and gets clobbered at some later point when it's needed)
  • The call is inlined, and the variable that it's returned from doesn't interact with anything in the callee, so the code for generating that return variable can be removed during a dead-code removal pass
  • The call is inlined and that return value is a pointer to something on the heap, in which case the stack pointer-liveness map for that point in the function will mark that pointer as dead (I'm not certain about this one) -- this might let the heap object get GC'd from that moment.
  • The call is assembly so it's eligible for neither the newer register-calling convention nor inlining, so the spot on the stack for that return value just gets ignored until the function returns (or that stack-space gets allocated to another variable)
  • cgo can't return multiple values, but if you ignore the return value in AMD64 calling conventions it's a register that can be ignored. (although it may be a pointer to something elsewhere in memory, in which case gets more interesting)
  • The call is inlined and the return value is a pointer which would normally be marked as escaping but because the pointer gets ignored, the compiler can go back to stack-allocating. (I'm not sure if the compiler has a second escape-analysis pass like this, but it could be interesting)

Marcel Huijkman <marcel....@semrush.com> schrieb am Sa. 5. Nov. 2022 um 09:18:
When I explain it during my training I always say it is a trashcan variable, anything you put in is to be ignored on the spot.

On Friday, November 4, 2022 at 10:10:20 PM UTC+1 Konstantin Khomoutov wrote:
On Fri, Nov 04, 2022 at 04:58:35AM -0700, Canuto wrote:

> I'm just starting out with go ...
> I have searched for lights on this string but without success.
> What does this sign mean " _, err " , what the underscore symbol means here?

If you're starting with Go, please start with the Go Tour [1].
For instance, the use of this underscore sign is covered in [2].

1. https://go.dev/tour/
2. https://go.dev/tour/moretypes/17

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/6c745c54-b212-4bb6-8e40-00273e6ee2fan%40googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages