1

Using Go, I am trying to send values from main to the function. I want to check if any parameter sent to function is empty. If there is any missing values, I want to print that the "Parameter value" is empty. If there are multiple parameters empty, I want to print that as well. If all parameters are correctly given, return the value.

func FederationValidarator(a string, b string) (string, string) {
    // var Messages []string
    rArray := [2]string{a, b}
    // i :=0
    for i := 0; i < len(rArray); i++ {
        if rArray[i] != "" {
            fmt.Println("Nothing is empty")
        } else {
            // var Messages []string
            fmt.Println("%s is Missing")
        }
    }
    return a, b
}

func main() {
    a, b := FederationValidarator("", "world")
    fmt.Println(a)
    fmt.Println(b)
}

How can i code to print missing values? I want to get the following output.

The results:

%s is Missing
Nothing is empty

world

Expected Output:

a is Missing
world
5
  • 1
    Your question is missing a question.
    – zerkms
    Jan 8, 2021 at 4:13
  • @zerkms... Thank you for suggestion.... I have updated the question. Please find reference
    – newbie dev
    Jan 8, 2021 at 4:19
  • 4
    if a == "" { fmt.Println("A is missing" } ?
    – zerkms
    Jan 8, 2021 at 4:32
  • @zerkms ... Yes, That is what i wanted to create.
    – newbie dev
    Jan 8, 2021 at 4:37
  • @zerkms.. Thank You for your response. I have updated the question and code as well as results that appears and expected output. Can you please look into it. Thank You.
    – newbie dev
    Jan 8, 2021 at 4:51

2 Answers 2

3

You can't get the parameter names (e.g. a), for details, see Getting method parameter names.

If you need the parameter names, wrap the parameters into a struct, and you can get the names of the fields. You may use reflection to iterate over the fields, and get their values and their names too.

For example:

type Param struct {
    A string
    B string
    C string
}

func CheckValues(p Param) {
    v := reflect.ValueOf(p)

    t := v.Type()
    for i := 0; i < v.NumField(); i++ {
        name := t.Field(i).Name
        if v.Field(i).IsZero() {
            fmt.Printf("%s is empty\n", name)
        } else {
            fmt.Printf("%s is NOT empty\n", name)
        }
    }
}

Testing it:

p := Param{"", "world", ""}
CheckValues(p)

Output (try it on the Go Playground):

A is empty
B is NOT empty
C is empty

One very nice property of this solution is that it does not depend on the actual parameter type. You may pass any struct values to it, and it will continue to work. Also it handles "all" field types, not just strings.

So modify the signature to this:

func CheckValues(p interface{})

And you may also pass anonymous structs, not just values of defined types:

a, b, c, d := "", "world", 0, 3

CheckValues(struct {
    A string
    B string
    C int
    D int
}{a, b, c, d})

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

A is empty
B is NOT empty
C is empty
D is NOT empty
0
0

This code is in order to match the result you expect. But it is not the correct way to check parameter names.

func FederationValidarator(a string, b string) (string, string) {
    
    rArray := [2]string{a, b}
    
    for i := 0; i < len(rArray); i++ {
        if string(rArray[i]) == "" {
            fmt.Printf("%s is Missing\n", rArray[i]) // %s is empty string, Then nothing will appear.
        } else {                        
           fmt.Println(rArray[i])
        }
    }    
    return a, b
}
 
func main() {
    _, _ = FederationValidarator("", "world")
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.