Go程序在具有简单的http.Get调用时没有输出时退出。

huangapple go评论78阅读模式
英文:

Go program exits with no output if it has a simple http.Get call

问题

ETA 6.14.2022. 对于未来的读者 - 罪魁祸首是我的杀毒软件,当我关闭它时它就能正常工作。我将不得不在杀毒软件中做一个例外。感谢那些花时间阅读、评论或研究此问题的人!

我是Go的新手,希望能得到一些关于运行一些看起来很简单的代码的帮助...(在Windows上)

这段代码不起作用:

package main

import (
	"fmt"
	"net/http"
)

func main() {
	fmt.Println("Hello World")

	response, err := http.Get("https://www.google.com/robots.txt")

	if err != nil {
		panic(err)
	}

	response.Body.Close()

	if err != nil {
		panic(err)
	}

	fmt.Println("Goodbye World")
}

通过“不起作用”我是指它只是退出而没有输出和错误,并且弹出一个新的提示行(没有挂起,就像运行了一个没有输出和完成的程序一样)。

这段代码起作用:

package main

import (
	"fmt"
)

func main() {
	fmt.Println("Hello World")

	fmt.Println("Goodbye World")
}

你会看到预期的Hello和Goodbye。

我在VS Code上运行这段代码,使用集成终端。

我也在Cmd Prompt和Powershell中运行类似的代码。

我使用的是Windows 10。

感谢任何建议!

P.S. 我确实意识到有些人认为在Windows上运行Go是在自找麻烦。但如果有人知道如何让这个看似简单的代码在Windows上运行,我会很感激。

ETA. 谢谢回复!

澄清一些要点:

1)我知道我没有从响应本身中打印任何内容。目前我只是想让简单的Hello和Goodbye语句与Get调用共存并打印出来。

2)我感谢确认这段代码应该在Windows上编译和运行。对于我应该在我的环境中寻找什么的任何想法都将不胜感激!

英文:

ETA 6.14.2022. For future readers - the culprit is my antivirus, as it works when I turn it off. I'll have to make an exception in the av. Thanks to those who took time to read, comment or research this!

I'm new to Go, and I'd appreciate some help with running some simple looking Code.... (on Windows however Go程序在具有简单的http.Get调用时没有输出时退出。 )

This code does not work

package main

import (
    "fmt"
	"net/http"
)

func main() {
    fmt.Println("Hello World")

    response, err := http.Get("https://www.google.com/robots.txt")

    if err != nil {
	    panic(err)
    }

    response.Body.Close()

    if err != nil {
	    panic(err)
    }

    fmt.Println("Goodbye World")

}

By "not working" I mean that it simply exits with no output and no error, and brings up a new prompt line (no hanging, just acts like it ran a program with no output and completed)

This code does work:

package main

import (
    "fmt"
)

func main() {
    fmt.Println("Hello World")

    fmt.Println("Goodbye World")

}

You'll see the expected Hello and Goodbye.

I'm running this on VS Code, using Integrated Terminal.

I've also run similar code outside VS Code in Cmd Prompt and Powershell.

I'm on Windows 10.

Appreciate any suggestions!

P.S. I do realize that some people argue that running Go on Windows is acting for trouble. But if anyone has information on how to get this apparently simple code to run on Windows, I'd appreciate it.

ETA. Thanks for the responses!

Points to clarify:

  1. I am aware I'm not printing anything from the response itself. At the moment I'm just trying to get the plain Hello And Goodbye statements to co-exist with the Get call and print out.

  2. I appreciate the confirmation that this code should compile and run on Windows. Any ideas on what I should look for in my environment would be appreciated!

答案1

得分: 1

好的,以下是翻译好的内容:

如果你期望从http.Get调用中得到任何输出,我可以告诉你为什么没有得到。你得到了一个响应对象,并且在没有读取它,更不用说打印它的情况下,就关闭了响应体。

你似乎还因为某种原因两次检查了err,这是没有意义的。至于hello worldgoodbye的消息,它们对我来说显示得很好,但这是如何打印http.Get的响应的方法:

package main

import (
    "fmt"
    "io/ioutil"
    "net/http"
)

func main() {
    response, err := http.Get("https://www.google.com/robots.txt")
    if err != nil {
        panic(err)
    }
    defer response.Body.Close() // 将此延迟到函数末尾
    body, err := ioutil.ReadAll(response.Body) // 读取数据
    if err != nil {
        panic(err)
    }
    fmt.Printf("Response body: %s\n\n", string(body))
    fmt.Println("All Done")
}

这样可以正常工作(在Linux和MacOS上尝试过,使用的是golang 1.18)。应该是可移植的,并且在Windows上也能正常工作。

英文:

Well, if you're expecting any output to come from the http.Get call, then I can tell you why you're not getting that. You're getting a response object, and closing the response body, without reading it, let alone printing it.

You also seem to be checking err twice for whatever reason, which doesn't make sense. I can't speak for the hello world and goodbye messages, they show up for me just fine, but this is how you would print the http.Get response:

package main

import (
    "fmt"
    "io/ioutil"
    "net/http"
)

func main() {
    response, err := http.Get("https://www.google.com/robots.txt")
    if err != nil {
        panic(err)
    }
    defer response.Body.Close() // defer this to the end of the function
    body, err := ioutil.ReadAll(response.Body) // read the data
    if err != nil {
        panic(err)
    }
    fmt.Printf("Response body: %s\n\n", string(body))
    fmt.Println("All Done")
}

That works just fine (tried on Linux and MacOS, golang 1.18. Should be portable, and work just fine on Windows, though).

huangapple
  • 本文由 发表于 2022年6月14日 01:07:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/72606594.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定