-
-
Save bvenkatr/9d13b241ce39cd163e0ea43f3ca9c840 to your computer and use it in GitHub Desktop.
golang wget
This file contains 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