|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?FreeOZ用户注册
x
本帖最后由 DDD888 于 10-9-2016 09:31 编辑
package utility
import (
"io/ioutil"
"net/http"
"time"
)
func GetHtmlFromUrl(websiteUrl string, timeoutValue int) (string, error) {
timeout := time.Duration(timeoutValue) * time.Second
client := http.Client{Timeout: timeout}
resp, err := client.Get(websiteUrl)
if err == nil {
reader := resp.Body
defer reader.Close()
body, err := ioutil.ReadAll(reader)
if err == nil {
return string(body), nil
}
}
return "", err
}
vs
func GetHtmlFromUrl(websiteUrl string, timeoutValue int) (string, error) {
timeout := time.Duration(timeoutValue) * time.Second
client := http.Client{
Timeout: timeout,
}
resp, err := client.Get(websiteUrl)
if err == nil {
reader := resp.Body
defer reader.Close()
body, err := ioutil.ReadAll(reader)
if err == nil {
return string(body), nil
}
}
return "", err
}
My question is why I need to add a comma at the the end like this, I was thinking that comma is useless in other language i.e. c#, it seems that my vi editor enforce me to do that, I guess the golang compiler might report error if I missed the comma
client := http.Client{
Timeout: timeout,
}
Any comments?
build environment
centos latest version
golang 1.7.1 |
|