我们如何用golang写条件?

I am writing the queries in the controller section but according to MVC structure the logic are in models and the controller section is used to send data only so in past I'm using like below conditions:-

models.Retrieve(bson.M{"_id": Id, "is_deleted": false})

//fucntion for this query is 
fucn Retrieve(query interface{}){

    // do stuff

}

But now Above query will change using mapping and I'm writing a function to use it for the multiple purpose for retrieving the data like:-

conditions := make(map[string]interface{})
conditions["operator1"] = "_id"
codnitions["value1"] = id
conditions["operator2"] = "is_deleted"
conditions["value2"] = false

func Retrieve(data map[string]interface{}){

//queries here

}

anyone tell me that this is a right way to this. if yes, then tell me how?

If no, Can you please tell me an example of this or suitable answer for my question.

Edited :-

This function is also used for the finding by the "code" means models.Retrieve(bson.M{"code": Code, "is_deleted": false})

It is also be written like that:-

conditions := make(map[string]interface{})
conditions["operator1"] = "code"
codnitions["value1"] = Code
conditions["operator2"] = "is_deleted"
conditions["value2"] = false

Thank you in advance.

If I understand correctly you want a function that will be able to handle different key value pairs as your query input. I implemented it in Go as follows:

type UserResolver int

// String satisfies the Stringer interface
func (ur UserResolver) String() string {
    strs := [...]string {
        "ID",
        "Code",
    }
    // return empty string if index out of bounds
    if int(ur) >= len(strs) {
        return ""
    }
    return strs[ur]
}

// Add any required functions to UserResolver that you may require,
// such as schemaKey() that returns the specified schema key value:
// In your example case "_id", "Code", "is_deleted", etc.

const (
    ID UserResolver = iota
    Code
)

func ResolveUser(by UserResolver, value interface{}) (User, error) {
    if by.String() == "" {
        return nil, fmt.Errorf("Unknown UserResolver specified")
    }

    if value == nil {
        return nil, fmt.Errorf("Nil value provided, unable to resolve User")
    }

    // Query details here:
    // It will be specific to your persistence model and remember to follow the
    // best practices for your specific db, such as properly escaping inputs...

    return User, nil
}

This approach allows you great flexibility by extending the UserResolver functionality with additional functions that may be exported or unexported (according to your API design). It basically maps various options into slices with a common index as defined in the const section using iota (automatic enumeration). You may now add functions that uses switch statements to do type or condition specific work.

The function can now be called by other packages as follows:

u, err := ResolveUser(user.ID, uid)
if err != nil {
    return fmt.Errorf(
        "User not found! Unable to obtain user by %s = %v",
        user.ID,
        uid,
    )
}

The conditions portion can be implemented similarly and then properly separates between the lookup index and the lookup conditions.