Home  >  Article  >  Backend Development  >  Sprintf with 3 arguments of different pointer types (can be nil). The ternary operator is not available, how to avoid writing dozens of lines?

Sprintf with 3 arguments of different pointer types (can be nil). The ternary operator is not available, how to avoid writing dozens of lines?

WBOY
WBOYforward
2024-02-12 20:20:091099browse

具有 3 个不同指针类型的参数(可以为 nil)的 Sprintf。三元运算符不可用,如何避免写几十行?

php editor Zimo is here to answer a question about the Sprintf function. Sometimes we need to use the Sprintf function to format a string, but in some cases, we may encounter a situation with parameters of three different pointer types, and these parameters may be nil. In this case, we cannot use the ternary operator, otherwise the code will become verbose and not easy to read. So, how should we avoid writing dozens of lines of lengthy code? Next, I will share with you a simple solution.

Question content

I want to use sprintf to create this string

message := fmt.sprintf("unit %s has a level of %v, but is of category %v",
    *entity.name, *entity.levelcode, *entity.categorycode)

In entities, variables are pointers, which can be nil:

  • name is *string
  • levelcode has *levelcode type
  • categorycode has *categorycode type

But if they have a value, I want the value instead of the pointer. (i.e. Unit abc has a level of zero but belongs to the management unit category)

No matter what language I use, I will write like this:

message := fmt.sprintf("unit %s has a level of %v, but is of %v category",
    entity.name != nil ? *entity.name : "nil", entity.levelcode != nil ? *entity.levelcode : "nil", entity.categorycode != nil ? *entity.categorycode : "nil")

But go does not allow the ternary operator. If I don't handle nil values, sprintf will throw an exception.

So, do I have to start like this?

if entity.Name == nil && entity.LevelCode != nil && entity.CategoryCode != nil) {
   message := "Unit nil has a Level of nil, but is of nil Category"
}
else {
   if entity.Name != nil && entity.LevelCode != nil && entity.CategoryCode != nil) {
     message := fmt.Sprintf("Unit %s has a Level of nil, but is of nil Category",
    entity.Name != nil ? *entity.Name : "nil")
   }
   else {
      ...

     for 9 combinations of values nil or not nil values, and 9 sprintf formats?
   }
}

What the shortest way to dump my variables content in a formatted line?

Solution

Thank you, with your help, I successfully built the function.

// value treat pointers that can be nil, and return their values if they aren't.
func value[t any](v *t) string {
    if (v != nil) {
        return fmt.sprintf("%v", *v)
    } else {
        return "nil"
    }
}

Called like this

message := fmt.Sprintf("Unit %s has a Level of %v, but is of %v Category",
    value(entity.Name), value(entity.LevelCode), value(entity.CategoryCode))

Write five statements for a single sprintf...but it works.

The above is the detailed content of Sprintf with 3 arguments of different pointer types (can be nil). The ternary operator is not available, how to avoid writing dozens of lines?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete