package main import ( "net/http" "fmt" "io/ioutil" "strings" ) func downloadJpg(url string,file_name string) { client := &http.Client{} req,err := http.NewRequest("GET",url,nil) if err != nil{ fmt.Println(err) } req.Header.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.108 Safari/537.2222") req.Header.Add("Referer","http://i1.shaodiyejin.com/") resp,err := client.Do(req) defer resp.Body.Close() byteCotent, err := ioutil.ReadAll(resp.Body) handError(err) ioutil.WriteFile(file_name, byteCotent, 0666) } func main() { url := "http://i1.shaodiyejin.com/uploads/tu/201909/9999/89130de80a.jpg" str1 := strings.Split(url, "/") file_name := str1[len(str1)-1] downloadJpg(url,"./imgs/"+file_name) } func handError(err error) { if err != nil{ fmt.Println("error",err) } }