Skip to content

Instantly share code, notes, and snippets.

@edermanoel94
Last active April 25, 2020 01:32
Show Gist options
  • Save edermanoel94/3eeb9a246a2bac53fbf121d6477d1f83 to your computer and use it in GitHub Desktop.
Save edermanoel94/3eeb9a246a2bac53fbf121d6477d1f83 to your computer and use it in GitHub Desktop.
read file
package main
import (
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"os"
"runtime"
"strconv"
"time"
)
func main() {
workGroup := New()
chSuccess := make(chan int, 0)
workGroup.Run(runtime.NumCPU()*2 - 1)
file, err := os.Open("./file.json")
if err != nil {
log.Println(err)
}
defer file.Close()
//size := int(4 * math.Pow(1024, 4))
data := make([]map[string]interface{}, 0)
err = json.NewDecoder(file).Decode(&data)
if err != nil {
log.Println(err)
}
for _, d := range data {
price, err := strconv.ParseFloat(d["price"].(string), 32)
if err == nil {
d["price"] = price
}
subscriptionId, err := strconv.ParseInt(d["subscriptionId"].(string), 10, 32)
if err == nil {
d["subscriptionId"] = subscriptionId
}
subscriptionOfferId, err := strconv.ParseInt(d["subscriptionOfferId"].(string), 10, 32)
if err == nil {
d["subscriptionOfferId"] = subscriptionOfferId
}
subscriptionServiceId, err := strconv.ParseInt(d["subscriptionServiceId"].(string), 10, 32)
if err == nil {
d["subscriptionServiceId"] = subscriptionServiceId
}
subscriptionParentServiceId, err := strconv.ParseInt(d["subscriptionParentServiceId"].(string), 10, 32)
if err == nil {
d["subscriptionParentServiceId"] = subscriptionParentServiceId
}
marshal, err := json.Marshal(&d)
if err != nil {
log.Println(err)
}
workGroup.Add(func() {
fmt.Println(string(marshal))
//send(bytes.NewReader(marshal), chSuccess)
})
}
//count := 0
//
//mutex := &sync.Mutex{}
//
//go func() {
// mutex.Lock()
// defer mutex.Unlock()
// count += <-chSuccess
//}()
workGroup.Wait()
}
func send(body io.Reader, chSuccess chan int) {
request, err := http.NewRequest(http.MethodPost, "<url aqui>", body)
if err != nil {
log.Println(err)
}
request.Header.Add("Authorization", "Basic <auth>")
client := http.Client{
Timeout: time.Minute * 2,
}
resp, err := client.Do(request)
if err != nil {
log.Println(err)
}
defer resp.Body.Close()
chSuccess <- resp.StatusCode
}
package main
type Function func()
type WorkGroup struct {
fn chan Function
allDone chan bool
}
func New() *WorkGroup {
return &WorkGroup{
fn: make(chan Function),
allDone: make(chan bool),
}
}
func (w *WorkGroup) Run(n int) {
workerIsDone := make(chan bool)
for i := 0; i < n; i++ {
go func() {
for fn := range w.fn {
fn()
}
workerIsDone <- true
}()
}
go func() {
for i := 0; i < n; i++ {
_ = <-workerIsDone
}
w.allDone <- true
}()
}
func (w *WorkGroup) Add(fn Function) {
w.fn <- fn
}
func (w *WorkGroup) Wait() {
close(w.fn)
_ = <-w.allDone
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment