-4

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 :-) )

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!

YvetteS
  • 1
  • 3
  • 3
    This has nothing to do with Windows, and Go is fully supported on Windows. – JimB Jun 13 '22 at 17:25
  • There is a problem unrelated to the code in the question. Both programs unconditionally write `Hello World` to stdout. –  Jun 13 '22 at 18:22

1 Answers1

1

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).

Elias Van Ootegem
  • 74,482
  • 9
  • 111
  • 149
  • Thanks for your response. I know I'm not printing anything out from the response.. at the moment I'm simply trying to get my Hello and Goodbye world to print out in a program where a Get call merely exists. For some reason it doesn't work on my machine – YvetteS Jun 13 '22 at 19:40