-3

I'm writing a small program in Go to check a server's status is up (out of a list of servers) by hostname. I have a function that iterates over a server type once they're all retrieved and stored from an http.GET into slice of type server.

I can log (commented out below) and see that both of these evaluate correctly:

// fmt.Printf("Server: %s - Status: %s\n", server.Name, status)
// fmt.Printf("%t\n", name == strings.ToLower(server.Name))

Both have the expected outcome but when I run the program, the fmt.Printf() call inside the if statement does not print to the console.

    func getServerStatus(name string) {
      servers := getAllServers()
      for _, server := range servers {
        status := boolToStatusString(server.Status)
        // fmt.Printf("Server: %s - Status: %s\n", server.Name, status)
        // fmt.Printf("%t\n", name == strings.ToLower(server.Name))
        if server.Name == strings.ToLower(name) {
          fmt.Printf("%s is %s", name, status)
        }
      }
    }

I've tried running this function as a goroutine with a channel of type string to store the result and call the Printf outside this function but anytime I go run main.go it just executes and prints nothing.

Solved: evaluating both as strings.ToLower in the if statement resolved the issue.

9
  • 2
    Does your program wait till the go routines finish?
    – mkopriva
    Commented Jan 22, 2018 at 15:41
  • Could you post how are you calling the function - getServerStatus ?
    – vedhavyas
    Commented Jan 22, 2018 at 15:42
  • For example this program doesn't wait for the go routine therefore you don't see nothing: play.golang.org/p/qKFq15T19oV
    – mkopriva
    Commented Jan 22, 2018 at 15:42
  • I experimented with a goroutine for this but the outcome was the same. It shouldn't require concurrent execution AFAIK since the http.Get is completed before the call.
    – hedrick
    Commented Jan 22, 2018 at 15:42
  • 1
    Don't switch the ToLower conversion arbitrarily play.golang.org/p/PzbhvXce-Gh
    – mkopriva
    Commented Jan 22, 2018 at 15:49

1 Answer 1

1

Turns out the evaluation was incorrect as I was not using strings.ToLower() on both causing the statement to always be false as the data was not sanitized before hand or at execution.

Credit to @mkopriva

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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