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

38번째 줄: 38번째 줄:


==같이 보기==
==같이 보기==
* [[같이보기]]
* [[Go 패키지 http]]


==참고==
==참고==

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

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 {
        // error 처리
        fmt.Println(err)
    }
    defer resp.Body.Close() // main 함수 끝날 때 닫기 예약
    body, err := ioutil.ReadAll(resp.Body)
    if err != nil {
        // error 처리
        fmt.Println(err)
    }
    fmt.Println(string(body))
}

2 같이 보기

3 참고

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