Home > Mobile >  Any difference between nil check left on right
Any difference between nil check left on right

Time:12-23

This is function takes two strings and returns struct or nil and I wrote a struct inside this function for use only this function.

type OrgFundingsDetailsFCT struct {
    ID           int     `db:"id"`
    OrgProfileID int     `db:"org_profile_id"`
    OrgID        int     `db:"org_id"`
    RefID        string  `db:"ref_id"`
    AmountUSD    float64 `db:"amount_usd"`
    FundingDate  string  `db:"funding_date"`
    Status       string  `db:"status"`
    Round        string  `db:"round"`
    CreatedBy    string  `db:"created_by"`
}
func (s *Server) getCompareOrgFundingsByRefID(refID, status string) (*OrgFundingsDetailsFCT, error) {
    type orgFunding struct {
        RefID  string `db:"ref_id"`
        Status string `db:"status"`
    }

    var orgFundingsDetailsFCT OrgFundingsDetailsFCT

    orgfunding := orgFunding{
        RefID:  refID,
        Status: status,
    }

    const query = `SELECT id,
                    org_profile_id,
                    org_id,
                    ref_id,
                    amount_usd,
                    funding_date,
                    status,round, 
                    created_by 
                    FROM org_fundings 
                    WHERE ref_id=:ref_id AND status=:status`

    if err := s.db.NamedGet(&orgFundingsDetailsFCT, query, orgfunding); err == sql.ErrNoRows {
        s.logger.Infof("empty rows! getCompareOrgFundingsByRefID #111 % v", err)
        return nil, nil
    } else if err != nil {
        s.logger.Infof("errors found! getCompareOrgFundingsByRefID#111  % v", err)
        return nil, err
    }
    return &orgFundingsDetailsFCT, nil
}

Now I'm checking if this function return nil like this

    if nil != orgFundingsRefIdPending{
// logic
}

But my question is if I check like that is it same or not?

    if orgFundingsRefIdPending != nil{
//logic
}

If nil left side and check with my result is right side OR, my result is left side and check with nil is right side, Is it same? Does that mean the same thing happens if I put ‍‍‍‍‍‍nil on either side? also if I use struct on use only function is it valid thing?

CodePudding user response:

The getCompareOrgFundingsByRefID() function returns a pointer and an error value. To check if the return value (the pointer) is nil, simply compare it to nil, e.g.:

var refID, status string
// Set input params
orgFundingsRefIdPending, err := getCompareOrgFundingsByRefID(refID, status)
if err != nil {
     // Handle error
}
if orgFundingsRefIdPending != nil {
    // Use orgFundingsRefIdPending
}

The == and != comparison operators can only be executed (their result can only be determined) if both of their operands are evaluated. Moreover, since these comparison operators are reflexive (meaning a == b is true only and only if b == a), the order does not matter. So a == b and b == a are equivalent.

The order matters if the operator would not be reflexive (e.g. < so a < b is not the same as b < a), or it could matter if not all operands would be needed for its result, such as the logical OR (||), because we know that if any of the operands of || is true, the result is true regardless of the other value. And since Go's || operator uses short-circuit evaluation (if the result is known before evaluating all operands, the rest are not evaluated, going from left-to-right), the order does matter in case of ||. E.g. in f() || g() if f() returns true, the g() function will not be called.

Note: Back to yoru case, if the returned pointer is not nil but you want to check if the pointed struct value is the zero value of its type, you may simply compare it to OrgFundingsDetailsFCT{}:

if orgFundingsRefIdPending != nil {
    // Use orgFundingsRefIdPending
    // Is it the zero value?
    if *orgFundingsRefIdPending == (OrgFundingsDetailsFCT{}) {
        // It's the zero value
    }
}

For details and more options, see How to check for an empty struct?

  • Related