Search code examples
gostatic-code-analysischannel

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.


Solution

  • 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