· 7 years ago · Sep 19, 2018, 09:20 AM
1func AddForm(url string) string {
2 if url == "" {
3 data := ReadByIoutil("file.txt") // the file contains several urls
4 array := ParseData(data) // parsing data in T "Object" => Object.url string
5 url = "https://"
6 url += array[rand.Intn(len(array))].url // choose randomly 1 among urls
7 log.Print(url)
8 }
9 resp, err := http.Get(url) //get content from url
10 check(err)
11 defer resp.Body.Close()
12 body, err := ioutil.ReadAll(resp.Body)
13 content := string(body)
14 WriteByOs("index.html", content) //save to another file (optionaly)
15 return content
16}
17
18// I practically don't understand this block, but I copied from another place but it works as I wanted
19func handler(w http.ResponseWriter, r *http.Request) {
20 url := r.FormValue("url")
21 if url == "" {
22 w.Header().Set("Content-Type", "text/html; charset=utf-8")
23 url = ""
24 //url = "habr.com"
25 fmt.Fprint(w, AddForm(url))
26 return
27 }
28}
29
302018/09/19 12:08:16 https://stackoverflow.com
312018/09/19 12:08:16 http: panic serving [::1]:51813: parse https://stackoverflow.com
32: invalid character "r" in host name
33goroutine 19 [running]:
34net/http.(*conn).serve.func1(0xc00003c8c0)
35 C:/Go/src/net/http/server.go:1746 +0xd7
36panic(0x665ae0, 0xc0002369f0)
37 C:/Go/src/runtime/panic.go:513 +0x1c7
38main.check(...)
39 C:/Users/dev/go/src/test/main.go:14
40main.AddForm(0xc000042800, 0x1b, 0x0, 0x0)
41 C:/Users/dev/go/src/test/main.go:100 +0x2e6
42main.handler(0x6f1300, 0xc0001447e0, 0xc0000d8c00)
43 C:/Users/dev/go/src/test/main.go:114 +0xb2
44net/http.HandlerFunc.ServeHTTP(0x6bfb20, 0x6f1300, 0xc0001447e0, 0xc0000d8c00)
45 C:/Go/src/net/http/server.go:1964 +0x4b
46net/http.(*ServeMux).ServeHTTP(0x899640, 0x6f1300, 0xc0001447e0, 0xc0000d8c00)
47 C:/Go/src/net/http/server.go:2361 +0x12e
48net/http.serverHandler.ServeHTTP(0xc000048c30, 0x6f1300, 0xc0001447e0, 0xc0000d8c00)
49 C:/Go/src/net/http/server.go:2741 +0xb2
50net/http.(*conn).serve(0xc00003c8c0, 0x6f1400, 0xc000040280)
51 C:/Go/src/net/http/server.go:1847 +0x64d
52created by net/http.(*Server).Serve
53 C:/Go/src/net/http/server.go:2851 +0x2fc
54
55google.com; yandex.ru; habr.com; xakep.ru; stackoverflow.com