-
-
Save udhos/8ccc53295eca9b3c1a7aa01a1b0928bf to your computer and use it in GitHub Desktop.
golang wget
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"context" | |
"flag" | |
"fmt" | |
"io/ioutil" | |
"log" | |
"net/http" | |
"time" | |
) | |
func HTTPGet(url string, timeout time.Duration) (content []byte, err error) { | |
request, err := http.NewRequest("GET", url, nil) | |
if err != nil { | |
return | |
} | |
ctx, cancel_func := context.WithTimeout(context.Background(), timeout) | |
request = request.WithContext(ctx) | |
response, err := http.DefaultClient.Do(request) | |
if err != nil { | |
return | |
} | |
defer response.Body.Close() | |
if response.StatusCode != 200 { | |
cancel_func() | |
return nil, fmt.Errorf("INVALID RESPONSE; status: %s", response.Status) | |
} | |
return ioutil.ReadAll(response.Body) | |
} | |
func main() { | |
url := flag.String("url", "http://news.163.com", "url") | |
timeout := flag.Duration("timeout", 7*time.Second, "超时时间") | |
output_path := flag.String("output", "test.txt", "输出文件路径") | |
flag.Parse() | |
content, err := HTTPGet(*url, *timeout) | |
if err != nil { | |
log.Fatalln("HTTPGET: ", err) | |
} | |
err = ioutil.WriteFile(*output_path, content, 0666) | |
if err != nil { | |
log.Fatalln("WriteFile: ", err) | |
} | |
return | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment