Original post

in the following code, if I put a wrong url then it panic error, in this case I don’t think defer received.Body.Close() ever get executed. Am I right? if I’m right, how can I make sure the underlining TCP get closed even in case of error (like giving wrong url)?

package main

import (
	"bufio"
	"fmt"
	"net/http"
)

func main(){
	received, err := http.Get("http://gobyexample.com")
	if err != nil {
		panic(err)
	}
	defer received.Body.Close()
	fmt.Println("status: ", received.Status)
	scanner := bufio.NewScanner(received.Body)
	for i := 0; scanner.Scan() && i < 5; i++{
		fmt.Println(scanner.Text())
	}
	if err := scanner.Err(); err != nil {
		panic(err)
	}
}

meanwhile, when I use defer recived.Body.Close() in goland it highlight it in yellow showing unhandled error, is there something wrong with defer received.Body.Close()?

How/where can I see the result of received.Body.Close()?