Magic go language (web download)

Source: Internet
Author: User

Magic go language (web download)


[Disclaimer: All Rights Reserved. You are welcome to reprint it. Do not use it for commercial purposes. Contact Email: feixiaoxing @ 163.com]


Currently, there are a lot of web page crawling code on the Internet. However, after reading the go language web download code, I found that its web page download code is the simplest. If you don't believe it, you can check it out,

package main import(    "fmt"    "log"    "net/http"    "os") func main(){    resp,err:=http.Get("http://www.baidu.com")    if err!=nil{        //handleerror        fmt.Println(err)        log.Fatal(err)    }    defer resp.Body.Close()    if resp.StatusCode==http.StatusOK{        fmt.Println(resp.StatusCode)    }    buf:=make([]byte,1024)    //createfile    f,err1:=os.OpenFile("baidu.html",os.O_RDWR|os.O_CREATE|os.O_APPEND,os.ModePerm)    if err1!=nil{        panic(err1)        return    }    defer f.Close()     for{        n,_:=resp.Body.Read(buf)        if 0==n{            break        }        f.WriteString(string(buf[:n]))    }}

You can pay attention to these functions, namely http. Get, OS. OpenFile, resp. Body. Read, f. WriteString. Imagine what these functions are used. As their names are described, their functions are http download, file creation, character reading, and file writing. No. If you are interested, you can copy and test the code.


If I remember correctly, this part of code is referenced elsewhere. You are welcome to contact me. I will comment on it later.




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.