用户名  找回密码
 FreeOZ用户注册
查看: 1702|回复: 0
打印 上一主题 下一主题

[学习深造] , golang question

[复制链接]
跳转到指定楼层
1#
发表于 10-9-2016 10:29:31 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有帐号?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
回复  

举报

您需要登录后才可以回帖 登录 | FreeOZ用户注册

本版积分规则

小黑屋|手机版|Archiver|FreeOZ论坛

GMT+11, 2-4-2025 15:09 , Processed in 0.015860 second(s), 17 queries , Gzip On, Redis On.

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表