Background
user@host curl -s http://stackoverflow.com | grep -m 1 stackoverflow.com
returns immediately if the string is found:
<meta name="twitter:domain" content="stackoverflow.com"/>
Aim
find a string on a website using Golang
Method
Based on sources from Go by Example and Schier's Blog the following code was created:
package main
import (
"fmt"
"io/ioutil"
"net/http"
"regexp"
)
func main() {
url := "http://stackoverflow.com"
resp, _ := http.Get(url)
bytes, _ := ioutil.ReadAll(resp.Body)
r, _ := regexp.Compile("stackoverflow.com")
fmt.Println(r.FindString(string(bytes)))
resp.Body.Close()
}
Results
Running the code results in:
stackoverflow.com
Discussion & Conclusions
- More code is required to achieve the same aim in Golang or is there a shorter solution
- Both options seems to return at the same time. Is static code in this case faster than dynamic code as well?
- I am concerned whether this code consumes too much memory. It should be used eventually to monitor hundreds of different websites