FreeOZ论坛

标题: , golang question [打印本页]

作者: DDD888    时间: 10-9-2016 09:29
标题: , golang question
本帖最后由 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论坛 (https://www.freeoz.org/ibbs/) Powered by Discuz! X3.2