ReadOperator.go
package main
import (
"fmt"
"math/big"
)
// ComparisonOperatorλ λ€μν λΉκ΅ μ°μ°μ μννλ ꡬ쑰체μ
λλ€.
type ComparisonOperator struct{}
// Eq ν¨μλ λ κ°μ΄ κ°μμ§ νμΈν©λλ€.
func (co *ComparisonOperator) Eq(vars []interface{}) bool {
left, right := co.parseVars(vars)
if left != nil && right != nil {
return left.Cmp(right) == 0
}
return vars[0] == vars[1]
}
// Ne ν¨μλ λ κ°μ΄ λ€λ₯Έμ§ νμΈν©λλ€.
func (co *ComparisonOperator) Ne(vars []interface{}) bool {
left, right := co.parseVars(vars)
if left != nil && right != nil {
return left.Cmp(right) != 0
}
return vars[0] != vars[1]
}
// Gt ν¨μλ 첫 λ²μ§Έ κ°μ΄ λ λ²μ§Έ κ°λ³΄λ€ ν°μ§ νμΈν©λλ€.
func (co *ComparisonOperator) Gt(vars []interface{}) bool {
left, right := co.parseVars(vars)
if left != nil && right != nil {
return left.Cmp(right) > 0
}
return vars[0].(string) > vars[1].(string)
}
// Gte ν¨μλ 첫 λ²μ§Έ κ°μ΄ λ λ²μ§Έ κ°λ³΄λ€ ν¬κ±°λ κ°μμ§ νμΈν©λλ€.
func (co *ComparisonOperator) Gte(vars []interface{}) bool {
left, right := co.parseVars(vars)
if left != nil && right != nil {
return left.Cmp(right) >= 0
}
return vars[0].(string) >= vars[1].(string)
}
// Lt ν¨μλ 첫 λ²μ§Έ κ°μ΄ λ λ²μ§Έ κ°λ³΄λ€ μμμ§ νμΈν©λλ€.
func (co *ComparisonOperator) Lt(vars []interface{}) bool {
left, right := co.parseVars(vars)
if left != nil && right != nil {
return left.Cmp(right) < 0
}
return vars[0].(string) < vars[1].(string)
}
// Lte ν¨μλ 첫 λ²μ§Έ κ°μ΄ λ λ²μ§Έ κ°λ³΄λ€ μκ±°λ κ°μμ§ νμΈν©λλ€.
func (co *ComparisonOperator) Lte(vars []interface{}) bool {
left, right := co.parseVars(vars)
if left != nil && right != nil {
return left.Cmp(right) <= 0
}
return vars[0].(string) <= vars[1].(string)
}
// parseVars ν¨μλ λ κ°μ *big.Float νμ
μΌλ‘ λ³νν©λλ€.
func (co *ComparisonOperator) parseVars(vars []interface{}) (*big.Float, *big.Float) {
left, ok1 := co.toBigFloat(vars[0])
right, ok2 := co.toBigFloat(vars[1])
if ok1 && ok2 {
return left, right
}
return nil, nil
}
// toBigFloat ν¨μλ κ°μ *big.Float νμ
μΌλ‘ λ³νν©λλ€.
func (co *ComparisonOperator) toBigFloat(val interface{}) (*big.Float, bool) {
switch v := val.(type) {
case int:
return big.NewFloat(float64(v)), true
case int64:
return big.NewFloat(float64(v)), true
case float64:
return big.NewFloat(v), true
case string:
f, _, err := big.ParseFloat(v, 10, 0, big.ToNearestEven)
if err == nil {
return f, true
}
}
return nil, false
}
func main() {
co := &ComparisonOperator{}
vars := []interface{}{"123.45", "123.45"}
fmt.Println("Eq:", co.Eq(vars)) // true
fmt.Println("Ne:", co.Ne(vars)) // false
fmt.Println("Gt:", co.Gt(vars)) // false
fmt.Println("Gte:", co.Gte(vars)) // true
fmt.Println("Lt:", co.Lt(vars)) // false
fmt.Println("Lte:", co.Lte(vars)) // true
}μ½λ μ€λͺ
main ν¨μ
Last updated