Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Go - What is the equivalent of Python's "pass"?

Tags:

python

select

go

I have a default cause in a select statement that I want to do nothing, just continue, but leaving the line blank stops anything in the statement from happening

        select {
        case quit_status := <-quit:
            if quit_status == true {
                fmt.Printf("********************* GOROUTINE [%d] Received QUIT MSG\n", id)
                return
            }
        default:
            fmt.Printf("GOROUTINE [%d] step: %d, NO QUIT MSG\n", id, i)
        }
like image 898
talloaktrees Avatar asked Sep 17 '12 01:09

talloaktrees


People also ask

What can I use instead of pass in Python?

With Python >= 3.0, you should use ... (Ellipsis) instead of pass to indicate "finish later" blocks.

Is there a pass in Golang?

Basic of Golang PointerPass by value will pass the value of the variable into the method, or we can say that the original variable 'copy' the value into another memory location and pass the newly created one into the method.

What is the pass in Python?

The pass statement is used as a placeholder for future code. When the pass statement is executed, nothing happens, but you avoid getting an error when empty code is not allowed. Empty code is not allowed in loops, function definitions, class definitions, or in if statements.

What is pass equivalent in C?

You don't have pass or equivalent keyword. But you can write equivalent code without any such keyword. def f(): pass. becomes void f() {} and class C: pass.


2 Answers

The default case in a select statement is intended to provide non-blocking I/O for channel reads and writes. The code in the default case is executed whenever none of the channels in any of the cases are ready to be read/written to.

So in your case, the default block is executed if the quit channel has nothing to say. You can simply remove the default case and it will block on the quit_status := <-quit case until a value is available in quit.. which is probably what you are after in this instance.

If you want to immediately continue executing code after the select statement, you should run this select statement in a separate goroutine:

go func() {
    select {
    case quit_status := <-quit:
        ...

    }
}()

// Execution continues here immediately.
like image 86
jimt Avatar answered Sep 18 '22 10:09

jimt


As @StephenWeinberg pointed out, there is no pass statement in go. Simply put nothing in the case when the channel hits something.

select {
    case <-ch:
        // do something
    case <-time.After(2*time.Second):
        // timeout
    default:
        // do nothing aka pass
}
like image 33
MaxCode Avatar answered Sep 19 '22 10:09

MaxCode