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

 
(사용자 2명의 중간 판 8개는 보이지 않습니다)
2번째 줄: 2번째 줄:
;Go http.Get()
;Go http.Get()


{{소스헤더|짧은 코드}}
<syntaxhighlight lang='go' run>
<syntaxhighlight lang='go' run>
package main
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))
}
</syntaxhighlight>


{{소스헤더|괜찮은 코드}}
import (
<syntaxhighlight lang='go' run>
"fmt"
package main
"io"
import "fmt"
"net/http"
import "net/http"
)
import "io/ioutil"
 
func main() {
func main() {
    resp, err := http.Get("https://raw.githubusercontent.com/jmnote/test1/master/utf8test.txt")
resp, err := http.Get("https://jsonplaceholder.typicode.com/todos/1")
    if err != nil {
if err != nil {
        // handle error
panic(err)
        fmt.Println(err)
}
    }
defer resp.Body.Close()
    defer resp.Body.Close() // 함수 끝날 때 닫기 예약
bodyBytes, err := io.ReadAll(resp.Body)
    body, err := ioutil.ReadAll(resp.Body)
if err != nil {
    if err != nil {
panic(err)
        // handle error
}
        fmt.Println(err)
fmt.Println(string(bodyBytes))
    }
    fmt.Println(string(body))
}
}
</syntaxhighlight>
</syntaxhighlight>


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


==참고==
==참고==
* https://golang.org/pkg/net/http/
* https://golang.org/pkg/net/http/


[[분류: Go]]
[[분류:Go http 패키지]]
[[분류:Import "fmt"]]
[[분류:Import "net/http"]]
[[분류:Import "io"]]

2024년 6월 26일 (수) 16:12 기준 최신판

1 개요[ | ]

Go http.Get()
package main

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

func main() {
	resp, err := http.Get("https://jsonplaceholder.typicode.com/todos/1")
	if err != nil {
		panic(err)
	}
	defer resp.Body.Close()
	bodyBytes, err := io.ReadAll(resp.Body)
	if err != nil {
		panic(err)
	}
	fmt.Println(string(bodyBytes))
}

2 같이 보기[ | ]

3 참고[ | ]

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