0

I am writing a script that connects to a NATS server. In this code I am trying to make it so that I can add multiple consumers to multiple different streams if needed, my question is whether or not it actually adds it to the stream if I use an underscore in the if statement. Also if anyone could help explain it in more detail.

consumerNames := map[string]string{
        "consumer-99": "test-subject-99",
        "consumer-100": "test-subject-100"
    }
    //loops through the Map above to add consumers to specific subjects on the stream
    for consumerName , subjectName := range consumerNames{
        if _, err := js.AddConsumer(streamConfig.Name, &nats.ConsumerConfig {
            Durable: consumerName,
            DeliverySubject: subjectName,
            AckPolicy: nats.AckExplicitPolicy
        } 
        ); err != nil {
            log.Fatalf("Can not add consumer to specified subject in stream: %v", err)
        }
    }
  • See https://go.dev/ref/spec#Blank_identifier. I recommend spending a few hours to learn the basics of a language before trying to modify code you have copy/pasted. – Kurtis Rader Jun 23 '23 at 19:11

1 Answers1

0

If you use an underscore, you are not interested in the returned value.

As i see, you only want to capture the error in case there is one, in you if statement, you always will execute the add consumer function call.

The only thing you need to understand there is that you cannot access to the info on the underscore info value. you alwayt execute the function and in case there is any error you log them

Leandro Toloza
  • 1,655
  • 1
  • 6
  • 24