Created
June 18, 2015 20:21
-
-
Save ARolek/d01c6adc6d152e1efab9 to your computer and use it in GitHub Desktop.
HTTP GET read body -> replace strings -> write to buffer
This file contains 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 ( | |
"bufio" | |
"bytes" | |
"log" | |
"net/http" | |
"strings" | |
) | |
func main() { | |
resp, err := http.Get("http://google.com/") | |
if err != nil { | |
log.Println(err) | |
} | |
// our replace rules | |
replacer := strings.NewReplacer( | |
"<div", "<foo", | |
) | |
// buffer to hold our output from the replacer | |
var buf bytes.Buffer | |
// scan the body and replace strings based on our reaplce rules | |
scanner := bufio.NewScanner(resp.Body) | |
for scanner.Scan() { | |
// apply replacer rules and save to our buffer | |
replacer.WriteString(&buf, scanner.Text()) | |
} | |
if err := scanner.Err(); err != nil { | |
log.Println("reading standard input:", err) | |
} | |
// our output with the modified string | |
log.Println(buf.String()) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment