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

2번째 줄: 2번째 줄:
;Go http.Get()
;Go http.Get()


{{소스헤더|짧은 코드}}
<syntaxhighlight lang='go' run>
<syntaxhighlight lang='go' run>
package main
package main
10번째 줄: 11번째 줄:
     resp, _ := http.Get("https://raw.githubusercontent.com/jmnote/test1/master/utf8test.txt")
     resp, _ := http.Get("https://raw.githubusercontent.com/jmnote/test1/master/utf8test.txt")
     body, _ := ioutil.ReadAll(resp.Body)
     body, _ := ioutil.ReadAll(resp.Body)
     fmt.Print(string(body))
     fmt.Println(string(body))
}
</syntaxhighlight>
 
{{소스헤더|괜찮은 코드}}
<syntaxhighlight lang='go' run>
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 := io.ReadAll(resp.Body)
    if err != nil {
        // handle error
        fmt.Println(err)
    }
    fmt.Println(string(body))
}
}
</syntaxhighlight>
</syntaxhighlight>

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 := io.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 }}