如何省略杜松子酒结构的一些参数

I have big structure with more than 50 params

type Application struct {
    Id                  int64   `json:"id"`
    FullName            string  `json:"fullName,omitempty"`
    ActualAddress       string  `json:"actualAddress,omitempty"`
.....
}

I use gin-gonic and when I return application I need to omit some params I've created a function which makes empty some params (playLink) and then gin returns me correct json (without unnecessary values). I heard that reflection isn't fast operation so in our case we can use a lot of ugly if-else or switch-cases. Is there any other solutions faster than reflecting and more beautiful than if-elses?

The thing is that structure params have non-empty values, so they wont by omitted by gin. That's why I've created function to make some params empty before return

The thing is, if you only want to zero a few fields, it's more readable to do it without a function, e.g.

app := Application{}

app.FullName, app.ActualAddress = "", ""

If you want to create a function for it, at least use variadic parameter, so it's easier to call it:

func zeroFields(application *Application, fields ...string) {
    // ...
}

So then calling it:

zeroFields(&app, "FullName", "ActualAddress")

Yes, this will have to use reflection, so it's slower than it could be, and error prone (mistyped names can only be detected at runtime). If you want to avoid using reflection, pass the address of the fields:

func zeroFields(ps ...*string) {
    for _, p := range ps {
        *p = ""
    }
}

This way you have compile-time guarantee that you type field names correctly, and that they have string type.

Calling it:

zeroFields(&application.FullName, &application.ActualAddress)

Try it on the Go Playground.

If I understand correctly: you want to return some values from your struct but not all of them? Perhaps a nested struct?

type Application struct {
  ID struct {
    ID int64 `json:"id"`
  } `json:"id"`
  Person struct {
    Fullname string `json:"Fullname"
  } `json:"person"
}

That should let you filter out the fields you want to use.