« Back to Index

[Go Range List and Goroutine Async Processing with Errors]

View original Gist on GitHub

Tags: #go #golang #concurrency #goroutines #thread #map

Go Range List and Goroutine Async Processing with Errors.go

package main

import (
	"errors"
	"fmt"
	"sync"
)

var count400 int = 1
var count404 int = 1
var count410 int = 1
var count500 int = 1

var mapping map[int]interface{} = map[int]interface{}{
	400: count400,
	404: count404,
	410: count410,
	500: count500,
}

func getPage(statusCode int) (string, error) {
	fmt.Println("statusCode:", statusCode, "count:", mapping[statusCode])

	typeAssertCountToInt := mapping[statusCode].(int)

	if mapping[statusCode] == 1 {
		mapping[statusCode] = typeAssertCountToInt + 1
		return "", errors.New("whoops 1")
	} else if mapping[statusCode] == 2 {
		mapping[statusCode] = typeAssertCountToInt + 1
		return "", errors.New("whoops 2")

	} else {
		return "yay", nil
	}
}

func preWarm(statusCode int, wg *sync.WaitGroup) string {
	page, err := getPage(statusCode)
	if err != nil {
		fmt.Println("error from get page:", statusCode , err)
		return preWarm(statusCode, wg)
	}

	fmt.Println("SUCCESS (", statusCode, ")", page)
	fmt.Println("now cache page", statusCode)
	wg.Done()
	return page

}

func main() {
	statusCodes := []int{400, 404, 410, 500}

	wg := &sync.WaitGroup{}
	wg.Add(len(statusCodes))

	for _, statusCode := range statusCodes {
		go preWarm(statusCode, wg)
	}

	wg.Wait()
}

/* OUTPUT: I think https://play.golang.org/p/ckWSx7yJEJE sequentializes the goroutines?


statusCode: 500 count: 1
error from get page: 500 whoops 1
statusCode: 500 count: 2
error from get page: 500 whoops 2
statusCode: 500 count: 3
SUCCESS ( 500 ) yay
now cache page 500

statusCode: 400 count: 1
error from get page: 400 whoops 1
statusCode: 400 count: 2
error from get page: 400 whoops 2
statusCode: 400 count: 3
SUCCESS ( 400 ) yay
now cache page 400

statusCode: 404 count: 1
error from get page: 404 whoops 1
statusCode: 404 count: 2
error from get page: 404 whoops 2
statusCode: 404 count: 3
SUCCESS ( 404 ) yay
now cache page 404

statusCode: 410 count: 1
error from get page: 410 whoops 1
statusCode: 410 count: 2
error from get page: 410 whoops 2
statusCode: 410 count: 3
SUCCESS ( 410 ) yay
now cache page 410
*/