FAQ

f, err := os.OpenFile(fileName, os.O_CREATE, 0666)

if err != nil {

panic(err)

}


defer f.Close()


var req http.Request

req.Method = "GET"

req.Close = true

req.URL, err = url.Parse(getUrl)

if err != nil {

panic(err)

}


header := http.Header{}

header.Set("User-Agent", UA)

req.Header = header

resp, err := http.DefaultClient.Do(&req)

if err == nil {

if resp.StatusCode == 200 {

fmt.Println(code + ":sucess")

io.Copy(f, resp.Body)

} else {

fmt.Println(code + ":" + strconv.Itoa(resp.StatusCode))

}

defer resp.Body.Close()

} else {

fmt.Println(code + ":error")

}

on linux,the created fileName is empty;but in windows, fileName has data
be written!i do not know why??



--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Search Discussions

  • Dave Cheney at Nov 6, 2013 at 7:58 am

    On Wed, Nov 6, 2013 at 6:47 PM, 刘辉 wrote:
    f, err := os.OpenFile(fileName, os.O_CREATE, 0666)

    if err != nil {

    panic(err)

    }


    defer f.Close()


    var req http.Request

    req.Method = "GET"

    req.Close = true

    req.URL, err = url.Parse(getUrl)

    if err != nil {

    panic(err)

    }


    header := http.Header{}

    header.Set("User-Agent", UA)

    req.Header = header

    resp, err := http.DefaultClient.Do(&req)

    if err == nil {

    if resp.StatusCode == 200 {

    fmt.Println(code + ":sucess")

    io.Copy(f, resp.Body)
    ^^ check the error code here

    http://golang.org/pkg/io/#Copy
    } else {

    fmt.Println(code + ":" + strconv.Itoa(resp.StatusCode))

    }

    defer resp.Body.Close()

    } else {

    fmt.Println(code + ":error")

    }
    on linux,the created fileName is empty;but in windows, fileName has data be
    written!i do not know why??



    --
    You received this message because you are subscribed to the Google Groups
    "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an
    email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • 刘辉 at Nov 11, 2013 at 8:19 am
    thinks, is ok! use
    f, err := os.OpenFile(fileName, os.O_RDWR|os.O_CREATE, 0666)
    and os.O_RDWR option

    在 2013年11月6日星期三UTC+8下午3时58分18秒,Dave Cheney写道:
    On Wed, Nov 6, 2013 at 6:47 PM, 刘辉 <ijib...@gmail.com <javascript:>>
    wrote:
    f, err := os.OpenFile(fileName, os.O_CREATE, 0666)

    if err != nil {

    panic(err)

    }


    defer f.Close()


    var req http.Request

    req.Method = "GET"

    req.Close = true

    req.URL, err = url.Parse(getUrl)

    if err != nil {

    panic(err)

    }


    header := http.Header{}

    header.Set("User-Agent", UA)

    req.Header = header

    resp, err := http.DefaultClient.Do(&req)

    if err == nil {

    if resp.StatusCode == 200 {

    fmt.Println(code + ":sucess")

    io.Copy(f, resp.Body)
    ^^ check the error code here

    http://golang.org/pkg/io/#Copy
    } else {

    fmt.Println(code + ":" + strconv.Itoa(resp.StatusCode))

    }

    defer resp.Body.Close()

    } else {

    fmt.Println(code + ":error")

    }
    on linux,the created fileName is empty;but in windows, fileName has data be
    written!i do not know why??



    --
    You received this message because you are subscribed to the Google Groups
    "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an
    email to golang-nuts...@googlegroups.com <javascript:>.
    For more options, visit https://groups.google.com/groups/opt_out.
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Alex Skinner at Nov 12, 2013 at 1:30 am
    Just in case you weren't aware, there is helper function os.Create that
    returns a file object with exactly the attributes you've written below.

    Alex
    On Monday, November 11, 2013 3:19:05 AM UTC-5, 刘辉 wrote:

    thinks, is ok! use
    f, err := os.OpenFile(fileName, os.O_RDWR|os.O_CREATE, 0666)
    and os.O_RDWR option

    在 2013年11月6日星期三UTC+8下午3时58分18秒,Dave Cheney写道:
    On Wed, Nov 6, 2013 at 6:47 PM, 刘辉 wrote:
    f, err := os.OpenFile(fileName, os.O_CREATE, 0666)

    if err != nil {

    panic(err)

    }


    defer f.Close()


    var req http.Request

    req.Method = "GET"

    req.Close = true

    req.URL, err = url.Parse(getUrl)

    if err != nil {

    panic(err)

    }


    header := http.Header{}

    header.Set("User-Agent", UA)

    req.Header = header

    resp, err := http.DefaultClient.Do(&req)

    if err == nil {

    if resp.StatusCode == 200 {

    fmt.Println(code + ":sucess")

    io.Copy(f, resp.Body)
    ^^ check the error code here

    http://golang.org/pkg/io/#Copy
    } else {

    fmt.Println(code + ":" + strconv.Itoa(resp.StatusCode))

    }

    defer resp.Body.Close()

    } else {

    fmt.Println(code + ":error")

    }
    on linux,the created fileName is empty;but in windows, fileName has data be
    written!i do not know why??



    --
    You received this message because you are subscribed to the Google Groups
    "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an
    email to golang-nuts...@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedNov 6, '13 at 7:47a
activeNov 12, '13 at 1:30a
posts4
users3
websitegolang.org

People

Translate

site design / logo © 2021 Grokbase