Last active
December 4, 2018 18:02
-
-
Save grantglidewell/3785fa997098a75fda81483374e0df99 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "fmt" | |
| "io/ioutil" | |
| "net/http" | |
| "strconv" | |
| "strings" | |
| "time" | |
| ) | |
| func main() { | |
| fmt.Println(scrape("URL", "SearchTerm")) | |
| } | |
| func scrape(u string, t string) (string, error) { | |
| // Make the http request for the resource | |
| resp, err := http.Get(u) | |
| if err != nil { | |
| return "", err | |
| } | |
| // Close the connection when the response is complete | |
| defer resp.Body.Close() | |
| // Read the response body to a variable | |
| body, err := ioutil.ReadAll(resp.Body) | |
| if err != nil { | |
| return "", err | |
| } | |
| // Create a unique token for filename | |
| ts := strconv.FormatInt(time.Now().UTC().UnixNano(), 10) | |
| // Generate a string for the filename | |
| filename := fmt.Sprintf("scrape%v.html", ts) | |
| // Write body as a file to disk | |
| ioutil.WriteFile(filename, body, 0644) | |
| // Read the file to a variable | |
| dat, err := ioutil.ReadFile(filename) | |
| if err != nil { | |
| return "", err | |
| } | |
| // Check if the search term is present | |
| if strings.Contains(string(dat), t) { | |
| // Return a message to the user that the search term was found | |
| return fmt.Sprintf("Found %[1]s in %[2]s", t, u), nil | |
| } | |
| // Return a message to the user that the search term was not found | |
| return fmt.Sprintf("%[1]s was not found in %[2]s", t, u), nil | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
scrape()should have the following signaturefunc scrape(u string, t string) (string, error)check()function. Replace it's invocations with:It seems repetitive but this is the the "Go way" is to perform error checks.
3. There is no need to create a new error variable. This goes back to point #1. If you ever receive an error in Go, you should return the error so you can discontinue execution. Hence all your variables should be named
err. Also for the sake of performance, don't allocate variables unncessarily.4. When appending strings to each other, you should use
fmt.Sprintforstrings.Builderfor large string concatenations. For example, replace line 30 withfln := fmt.Sprintf("scrape%v.html", ts).5. I know Go has variable naming conventions, but something like
bdyshould be namedbody,flnbe namedfilenameand so forth.6. Perform a
nilcheck on line 35.