第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會有你想問的

是否可以從字符串創(chuàng)建比較運(yùn)算符?

是否可以從字符串創(chuàng)建比較運(yùn)算符?

Go
紅顏莎娜 2022-04-20 20:44:28
我正在嘗試創(chuàng)建一個(gè)函數(shù),該函數(shù)將從預(yù)定義的數(shù)組中生成一個(gè) if 條件。例如:package errorstype errorCase struct {    // This is the field I need to get in another struct    Field        string    // The comparison operator    TestOperator string    // The value that the expected one should not with equal...    WrongValue   interface{}}var ErrorCases = []*errorCase{ {    "MinValue",    "<",    0,}, {    "MaxValue",    "==",    0,}}實(shí)際上,我使用 for 循環(huán)創(chuàng)建了一個(gè)新函數(shù),該循環(huán)遍歷所有這些“錯(cuò)誤案例”func isDirty(questionInterface models.QuestionInterface) bool {    for _, errorCase := range errors.ErrorCases {        s := reflect.ValueOf(&questionInterface).Elem()        value := s.Elem().FieldByName(errorCase.Field)        // At this point I need to create my if condition        // to compare the value of the value var and the wrong one        // With the given comparison operator    }    // Should return the comparison test value    return true}是否有可能創(chuàng)建這樣的 if 條件?用反射包?我認(rèn)為這是可能的,但我沒有找到我應(yīng)該從哪里開始。
查看完整描述

1 回答

?
慕妹3242003

TA貢獻(xiàn)1824條經(jīng)驗(yàn) 獲得超6個(gè)贊

這個(gè)有可能。我以前建立了一個(gè)像這樣的通用比較庫。

簡單來說,比較包含 3 個(gè)部分:

  1. 比較左側(cè)的某種值。

  2. 運(yùn)算符( , =<>...)。

  3. 比較右側(cè)的某種值。

這 3 個(gè)部分僅包含兩種不同的類型 - valueoperator。我試圖將這兩種類型抽象為它們的基本形式。

  • value可以是任何東西,所以我們使用空接口 - interface{}。

  • 運(yùn)算符是有限集的一部分,每個(gè)都有自己的規(guī)則。

type Operator int


const (

    Equals Operator = 1

)

用符號評估比較=只有一個(gè)規(guī)則是有效的——兩個(gè)值應(yīng)該是相同的類型。你不能比較1和hello。之后,您只需確保值相同。


我們可以實(shí)現(xiàn)一個(gè)新的元類型來包裝評估一個(gè)operator.


// Function signature for a "rule" of an operator.

type validFn func(left, right interface{}) bool


// Function signature for evaluating an operator comparison.

type evalFn func(left, right interface{}) bool


type operatorMeta struct {

    valid []validFn

    eval  evalFn

}

現(xiàn)在我們已經(jīng)定義了我們的類型,我們需要實(shí)現(xiàn)規(guī)則和比較函數(shù)Equals。


func sameTypes(left, right interface{}) bool {

    return reflect.TypeOf(left).Kind() == reflect.TypeOf(right).Kind()

}


func equals(left, right interface{}) bool {

    return reflect.DeepEqual(left, right)

}

驚人的!所以我們現(xiàn)在可以驗(yàn)證我們的兩個(gè)值是否屬于同一類型,如果是,我們可以將它們相互比較。難題的最后一塊,是將運(yùn)算符映射到其適當(dāng)?shù)囊?guī)則和評估,并具有執(zhí)行所有這些邏輯的函數(shù)。


var args = map[Operator]operatorMeta{

    Equals: {

        valid: []validFn{sameTypes},

        eval:  equals,

    },

}


func compare(o Operator, left, right interface{}) (bool, error) {

    opArgs, ok := args[o]

    if !ok {

        // You haven't implemented logic for this operator.

    }


    for _, validFn := range opArgs.valid {

        if !validFn(left, right) {

            // One of the rules were not satisfied.

        }

    }


    return opArgs.eval(left, right), nil

}

讓我們總結(jié)一下到目前為止我們所做的:

  • 將基本比較抽象為valueoperator。

  • 創(chuàng)建了一種方法來驗(yàn)證一對是否對運(yùn)算符有效。

  • 在給定兩個(gè)的情況下,創(chuàng)建了一種評估運(yùn)算符的方法。

我希望我對您如何解決這個(gè)問題有所了解。這是一個(gè)簡單的想法,但可能需要一些樣板才能正常工作。

祝你好運(yùn)!


查看完整回答
反對 回復(fù) 2022-04-20
  • 1 回答
  • 0 關(guān)注
  • 112 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號