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

1

12

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.

Probe answered 30/5, 2021 at 21:11 Comment(0)
M
30

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
Mccormac answered 30/5, 2021 at 21:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.