doufu1504 2018-01-22 15:39
浏览 66

fmt.Printf不在循环内执行

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
", server.Name, status)
// fmt.Printf("%t
", 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
", server.Name, status)
        // fmt.Printf("%t
", 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.

  • 写回答

1条回答 默认 最新

  • douxi6903 2018-01-22 17:10
    关注

    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

    评论

报告相同问题?

悬赏问题

  • ¥100 iOS开发关于快捷指令截屏后如何将截屏(或从截屏中提取出的文本)回传给本应用并打开指定页面
  • ¥15 unity连接Sqlserver
  • ¥15 图中这种约束条件lingo该怎么表示出来
  • ¥15 VSCode里的Prettier如何实现等式赋值后的对齐效果?
  • ¥15 流式socket文件传输答疑
  • ¥20 keepalive配置业务服务双机单活的方法。业务服务一定是要双机单活的方式
  • ¥50 关于多次提交POST数据后,无法获取到POST数据参数的问题
  • ¥15 win10,这种情况怎么办
  • ¥15 如何在配置使用Prettier的VSCode中通过Better Align插件来对齐等式?(相关搜索:格式化)
  • ¥100 在连接内网VPN时,如何同时保持互联网连接