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 (

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

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()?