如何在Go语言中高效地编写多个if条件语句?

huangapple go评论75阅读模式
英文:

How to make multiple if-conditions efficiently in Go?

问题

我必须检查一组变量,看它们是否包含空字符串作为值。如果字符串为空,则应将其设置为默认值"no value"。在Golang中,有什么高效的方法来处理这个问题?

例如:

func defaultValue(myObject) {
	if myObject.name == "" {
		myObject.name = "no value"
	}
	if myObject.color == "" {
		myObject.color = "no value"
	}
	if myObject.typing == "" {
		myObject.typing = "no value"
	}
	if myObject.price == "" {
		myObject.price = "no value"
	}
}

我知道可以使用简单的if语句来实现,但我想知道是否有更高效的方法。

英文:

I have to check a set of variables, if they contain an empty string as value. If the string is empty, it should be set to a default value of "no value". What is a efficient way to handle this in Golang?

Example given:

func defaultValue (myObject) {
	if myObject.name == "" {
		myObject.name = "no value"
	}
	if myObject.color == "" {
		myObject.color = "no value"
	}
	if myObject.typing == "" {
		myObject.typing = "no value"
	}
	if myObject.price == "" {
		myObject.price = "no value"
	}
}

I know how to do it with simple if statements, but I am wondering if there is an approach more efficient to this.

答案1

得分: 3

你可以为此创建一个单独的函数

func optional(input string) string {
   if input == "" {
       return "no value"
   }
   return input
}

func defaultValue (myObject) {
    myObject.name = optional(myObject.name)
    myObject.color = optional(myObject.color)
    myObject.typing = optional(myObject.typing)
    myObject.price = optional(myObject.price)
}
英文:

You can create a separate function for that

func optional(input string) string {
   if input == "" {
       return "no value"
   }
   return input
}

func defaultValue (myObject) {
    myObject.name = optional(myObject.name)
    myObject.color = optional(myObject.color)
    myObject.typing = optional(myObject.typing)
    myObject.price = optional(myObject.price)
}

答案2

得分: 2

func aorb[T comparable](a, b T) T {
	var z T
	if a != z {
		return a
	}
	return b
}
func defaultValue(o *myObject) {
	o.name = aorb(o.name, "无值")
	o.color = aorb(o.color, "无值")
	o.typing = aorb(o.typing, "无值")
	o.price = aorb(o.price, "无值")
}
英文:
func aorb[T comparable](a, b T) T {
	var z T
	if a != z {
		return a
	}
	return b
}
func defaultValue(o *myObject) {
	o.name = aorb(o.name, "no value")
	o.color = aorb(o.color, "no value")
	o.typing = aorb(o.typing, "no value")
	o.price = aorb(o.price, "no value")
}

答案3

得分: 1

你可以创建一个通用的辅助函数来检查特定字段。如果你传递字段的地址,该辅助函数还可以设置默认值。使用可变参数,你甚至可以列出多个字段(如果它们应该具有相同的默认值):

func check[T comparable](def T, fields ...*T) {
    var zero T
    for _, field := range fields {
        if *field == zero {
            *field = def
        }
    }
}

测试一下:

type Obj struct {
    name, color, typing, price string
}

func main() {
    o := Obj{color: "red"}
    fmt.Printf("%#v\n", o)
    check("no value", &o.name, &o.color, &o.typing, &o.price)
    fmt.Printf("%#v\n", o)
}

这将输出(在Go Playground上尝试一下):

main.Obj{name:"", color:"red", typing:"", price:""}
main.Obj{name:"no value", color:"red", typing:"no value", price:"no value"}
英文:

You can create a generic helper that checks a specific field. If you pass the address of the field, the helper can also set the default value. Using variadic argument you can even list multiple fields (if they should have the same default value):

func check[T comparable](def T, fields ...*T) {
	var zero T
	for _, field := range fields {
		if *field == zero {
			*field = def
		}
	}
}

Testing it:

type Obj struct {
	name, color, typing, price string
}

func main() {
	o := Obj{color: "red"}
	fmt.Printf("%#v\n", o)
	check("no value", &o.name, &o.color, &o.typing, &o.price)
	fmt.Printf("%#v\n", o)
}

This will output (try it on the Go Playground):

main.Obj{name:"", color:"red", typing:"", price:""}
main.Obj{name:"no value", color:"red", typing:"no value", price:"no value"}

huangapple
  • 本文由 发表于 2023年2月3日 19:53:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/75335351.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定