I'm new to Go and trying to generate multiple requests to several http/ https servers to check the response time and the status of each web server.
I stored the URLs in a text file, afterwards I decided to add a ticker to my code which will keep generating these requests on each URL after a certain amount of time (the amount of time is in seconds typed next to each URL and spaced with a tab).
When I started scanning the time from the file everything got complicated and I can't manage to find my mistake. Here is my Go code:
package main
import (
"fmt"
"io/ioutil"
"log"
"net/http"
"strconv"
"strings"
"sync"
"time"
)
func get_resp_time(url string) { //Get time for each URL
time_start := time.Now()
fmt.Println("Start time", time_start, " URL ", url)
resp, err := http.Get(url)
//fmt.Printf("resp : %#v
", resp)
if err != nil {
log.Printf("Error fetching: %v", err)
}
defer resp.Body.Close()
fmt.Println(time.Since(time_start), url, " Status: ", resp.Status)
}
func main() {
content, _ := ioutil.ReadFile("url_list.txt")
lines := strings.Split(string(content), "\t")
//fields := strings.Split(string(content), "\t")
//fmt.Println(lines[1])
//fmt.Println(strconv.Atoi(lines[0]))
const workers = 25
var nb int
wg := new(sync.WaitGroup)
in := make(chan string, 2*workers)
if _, err := strconv.Atoi(lines[1]); err == nil {
nb, err = strconv.Atoi(lines[1])
}
ticker := time.NewTicker(time.Second * time.Duration(nb))
for t := range ticker.C {
fmt.Println("Time of origin: ", time.Now())
for i := 0; i < len(lines)-1; i++ {
wg.Add(1)
go func() {
defer wg.Done()
//for j := 0; j < len(in); j++ {
if _, err := strconv.Atoi(lines[i]); err == nil {
nb, err = strconv.Atoi(lines[i])
//get_resp_time(url)
} else {
get_resp_time(lines[i])
}
//}
}()
}
for _, url := range lines {
if url != "" {
in <- url
}
}
fmt.Println("Tick at ", t)
}
close(in)
wg.Wait()
}
And the text file:
http://google.com 5
http://nike.com 10
This is the error I got:
panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xb code=0x1 addr=0x0 pc=0x2752]
goroutine 9 [running]:
panic(0x313180, 0xc82000a0d0)
/usr/local/go/src/runtime/panic.go:464 +0x3e6
main.get_resp_time(0xc82006e195, 0x14)
/Users/Elliott/Desktop/GoTutorial/url-time-response.go:26 +0x712
main.main.func1(0xc820070dc0, 0xc82006e1e0, 0xc8200a0090, 0x9, 0x9, 0xc820070da8)
/Users/Elliott/Desktop/GoTutorial/url-time-response.go:61 +0x13f
created by main.main
/Users/Elliott/Desktop/GoTutorial/url-time-response.go:65 +0x4a6
exit status 2
EDIT: Alright, I just changed the file to having only one duration because apparently it's pretty complicated to have multiple duration(according to my project advisor). Thanks for the help!
You don't appear to be parsing your file correctly. You should first split on , THEN split on
\t
. Also, verify your text file actually has \t
and not spaces.
You should probably iterate through your parsed values with fmt.Println
to verify your parsed results are as expected.
It should look something like this.