github.com/karrick/go@v0.0.0-20170817181416-d5b0ec858b37/src/cmd/vet/testdata/httpresponse.go (about)

     1  package testdata
     2  
     3  import (
     4  	"log"
     5  	"net/http"
     6  )
     7  
     8  func goodHTTPGet() {
     9  	res, err := http.Get("http://foo.com")
    10  	if err != nil {
    11  		log.Fatal(err)
    12  	}
    13  	defer res.Body.Close()
    14  }
    15  
    16  func badHTTPGet() {
    17  	res, err := http.Get("http://foo.com")
    18  	defer res.Body.Close() // ERROR "using res before checking for errors"
    19  	if err != nil {
    20  		log.Fatal(err)
    21  	}
    22  }
    23  
    24  func badHTTPHead() {
    25  	res, err := http.Head("http://foo.com")
    26  	defer res.Body.Close() // ERROR "using res before checking for errors"
    27  	if err != nil {
    28  		log.Fatal(err)
    29  	}
    30  }
    31  
    32  func goodClientGet() {
    33  	client := http.DefaultClient
    34  	res, err := client.Get("http://foo.com")
    35  	if err != nil {
    36  		log.Fatal(err)
    37  	}
    38  	defer res.Body.Close()
    39  }
    40  
    41  func badClientPtrGet() {
    42  	client := http.DefaultClient
    43  	resp, err := client.Get("http://foo.com")
    44  	defer resp.Body.Close() // ERROR "using resp before checking for errors"
    45  	if err != nil {
    46  		log.Fatal(err)
    47  	}
    48  }
    49  
    50  func badClientGet() {
    51  	client := http.Client{}
    52  	resp, err := client.Get("http://foo.com")
    53  	defer resp.Body.Close() // ERROR "using resp before checking for errors"
    54  	if err != nil {
    55  		log.Fatal(err)
    56  	}
    57  }
    58  
    59  func badClientPtrDo() {
    60  	client := http.DefaultClient
    61  	req, err := http.NewRequest("GET", "http://foo.com", nil)
    62  	if err != nil {
    63  		log.Fatal(err)
    64  	}
    65  
    66  	resp, err := client.Do(req)
    67  	defer resp.Body.Close() // ERROR "using resp before checking for errors"
    68  	if err != nil {
    69  		log.Fatal(err)
    70  	}
    71  }
    72  
    73  func badClientDo() {
    74  	var client http.Client
    75  	req, err := http.NewRequest("GET", "http://foo.com", nil)
    76  	if err != nil {
    77  		log.Fatal(err)
    78  	}
    79  
    80  	resp, err := client.Do(req)
    81  	defer resp.Body.Close() // ERROR "using resp before checking for errors"
    82  	if err != nil {
    83  		log.Fatal(err)
    84  	}
    85  }