jeudi 9 janvier 2020

string.Contains function is not working as expected in my golang code. Though first string contains the second string, condition fails

I am checking some shell command output in golang. I need to check whether the output contains my input string.

//myinput is a valid string
 stdoutput2,stderr2,err2 := Shellout("some valid shell command | grep "+myinput)
        if(err2!=nil){
        glg.Error("Not able to retrive information from system")
        glg.Error(err2)
        return false
        } else if strings.Contains(strings.TrimSpace(stdoutput2),myinput){
        glg.Info("able to retrive information from system")
        return true
      } else {
                glg.Error("Not able to retrive information from system")
                glg.Error("stdoutput2 after trimming is-->"+strings.TrimSpace(stdoutput2))
                glg.Error("myinput is :"+myinput)
                glg.Error("std error is :"+stderr2)
                return false
        } 

Here though stdoutput2 contains the value of myinput, my else if condition is failing.

The final output is

Not able to retrive information from system. stdoutput2 after trimming is--> Name = customcsi-b35f3ea733 myinput is :customcsi-b35f3ea733


Please note the space between Name and = symbol. That is part of output.

Please help in resolving this. My golang version is 1.13.5

Any help would be much appreciated.

Aucun commentaire:

Enregistrer un commentaire