package main import ( "fmt" "log" "net/http" "os" ) func main() { resp, err := http.Get("http://domain/path") if err != nil { fmt.Println(err) log.Fatal(err) } if resp.StatusCode == http.StatusOK { fmt.Println(resp.StatusCode) } defer resp.Body.Close() buf := make([]byte, 1024) f, err1 := os.OpenFile("path.txt", 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])) } }
有疑问加站长微信联系(非本文作者)