DDD888 发表于 10-9-2016 09:29:31

, 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? :loveliness:

build environment
centos latest version
golang 1.7.1
页: [1]
查看完整版本: , golang question