"Go http.Get()"의 두 판 사이의 차이

28번째 줄: 28번째 줄:
     }
     }
     defer resp.Body.Close()
     defer resp.Body.Close()
     body, err := io.ReadAll(resp.Body)
     body, err := ioutil.ReadAll(resp.Body)
     if err != nil {
     if err != nil {
         // handle error
         // handle error

2021년 4월 5일 (월) 18:02 판

1 개요

Go http.Get()
짧은 코드
package main
import "fmt"
import "net/http"
import "io/ioutil"
func main() {
    resp, _ := http.Get("https://raw.githubusercontent.com/jmnote/test1/master/utf8test.txt")
    body, _ := ioutil.ReadAll(resp.Body)
    fmt.Println(string(body))
}
괜찮은 코드
package main
import "fmt"
import "net/http"
import "io/ioutil"
func main() {
    resp, err := http.Get("https://raw.githubusercontent.com/jmnote/test1/master/utf8test.txt")
    if err != nil {
        // handle error
        fmt.Println(err)
    }
    defer resp.Body.Close()
    body, err := ioutil.ReadAll(resp.Body)
    if err != nil {
        // handle error
        fmt.Println(err)
    }
    fmt.Println(string(body))
}

2 같이 보기

3 참고

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}