Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

go-staticcheck: should use a simple channel send/receive instead of select with a single case (S1000)

I am using Go 1.16.4. I am trying to deal with such code:

func (pool *myConnPool) GetPooledConnection() (*myConnection, error) {
    go func() {
        conn, err := pool.createConn()
        if err != nil {
            return
        }
        pool.connections <- conn
    }()
    select { // <<<< golint warning here
    case conn := <-pool.connections:
        return pool.packConn(conn), nil
    }
}

I am getting following Go linter warning: should use a simple channel send/receive instead of select with a single case (S1000) at the point marked in the code. Can anyone please explain how to fix that? I am not yet too experienced with Go channels.

like image 959
ivan.ukr Avatar asked Dec 11 '25 22:12

ivan.ukr


1 Answers

The linter is telling you that your use of select is meaningless with only a single case. To solve the problem, replace this:

select {
case conn := <-pool.connections:
    return pool.packConn(conn), nil
}

With:

conn := <-pool.connections
return pool.packConn(conn), nil

Or even:

return pool.packConn(<-pool.connections), nil
like image 158
Flimzy Avatar answered Dec 14 '25 13:12

Flimzy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!