Skip to content

Instantly share code, notes, and snippets.

@ARolek
Created June 18, 2015 20:21
Show Gist options
  • Save ARolek/d01c6adc6d152e1efab9 to your computer and use it in GitHub Desktop.
Save ARolek/d01c6adc6d152e1efab9 to your computer and use it in GitHub Desktop.
HTTP GET read body -> replace strings -> write to buffer
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