Home > Backend Development > Golang > 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
Release: 2024-02-12 20:20:09
forward
1219 people have browsed it

具有 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)
Copy after login

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")
Copy after login

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?
Copy after login

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"
    }
}
Copy after login

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))
Copy after login

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!

source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template