Skip to content

Instantly share code, notes, and snippets.

@stnc
Last active December 17, 2017 16:49
Show Gist options
  • Select an option

  • Save stnc/b949c2a05eda2a57f8500ea40780ddcd to your computer and use it in GitHub Desktop.

Select an option

Save stnc/b949c2a05eda2a57f8500ea40780ddcd to your computer and use it in GitHub Desktop.
go lang crawler concurrency and sync example
package main
import (
"database/sql"
"fmt"
"io/ioutil"
"net/http"
"strconv"
"sync"
"time"
_ "github.com/go-sql-driver/mysql"
)
//NewWriteSQLVersion ddd
func NewWriteSQLVersion(catPages string, cat_id int) {
db, err := sql.Open("mysql", "root:@/crawler?charset=utf8")
checkErr(err)
// insert
stmt, err := db.Prepare("INSERT i_rehber SET kategori_sayfalari=?,kategori_id=?,add_date=?")
checkErr(err)
res, err := stmt.Exec(catPages, cat_id, time.Now())
checkErr(err)
fmt.Print(res)
}
func checkErr(err error) {
if err != nil {
panic(err)
}
}
func main() {
var kategoriID = 6
var kac_sayfa = 4
var urladres = "http://www.example.com?_"
var urls [4]string
for i := 0; i < kac_sayfa; i++ {
new := i * 20
t := strconv.Itoa(new)
urls[i] = urladres + t + ".html"
}
fmt.Print(urls)
var wg sync.WaitGroup
wg.Add(len(urls))
for _, url := range urls {
go func(url string) {
defer wg.Done()
response, err := http.Get(url)
if err != nil {
// error handling
}
// fmt.Printf("%s: %s\n",url,response.Status)
defer response.Body.Close()
body, err := ioutil.ReadAll(response.Body)
NewWriteSQLVersion(string(body), kategoriID)
fmt.Print(string(body))
}(url)
}
wg.Wait() // waits until the url checks complete
fmt.Print(urls)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment