Task: Rosetta Code_Count examples



C#: using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Net;

class Task {
    private string _task;
    private int _examples;

    public Task(string task, int examples) {
        _task = task;
        _examples = examples;
    }

    public string Name {
        get { return _task; }
    }

    public int Examples {
        get { return _examples; }
    }

    public override string ToString() {
        return String.Format("{0}: {1} examples.", this._task, this._examples);
    }
}

class Program {
    static List<string> GetTitlesFromCategory(string category, WebClient wc) {
        string content = wc.DownloadString(
            String.Format("http:
        );

        return new Regex("\"title\":\"(.+?)\"").Matches(content).Cast<Match>().Select(x => x.Groups[1].Value.Replace("\\/", "/")).ToList();
    }

    static string GetSourceCodeFromPage(string page, WebClient wc) {
        return wc.DownloadString(
            String.Format("http:
        );
    }

    static void Main(string[] args) {
        WebClient wc = new WebClient();
        List<Task> tasks = new List<Task>();
        List<string> tasknames = GetTitlesFromCategory("Programming_Tasks", wc);

        foreach (string task in tasknames) {
            try {
                string content = GetSourceCodeFromPage(WebUtility.UrlEncode(task), wc);
                int count = new Regex("=={{header", RegexOptions.IgnoreCase).Matches(content).Count;
                Task t = new Task(task, count);

                Console.WriteLine(t);
                tasks.Add(t);
            }
            catch (Exception ex) {
                Console.WriteLine("****            Unable to get task \"" + task + "\": " + ex.Message);
            }
        }

        Console.WriteLine("\nTotal: {0} examples.", tasks.Select(x => x.Examples).Sum());
    }
}


Go: package main

import (
    "bytes"
    "encoding/xml"
    "fmt"
    "io"
    "io/ioutil"
    "net/http"
    "net/url"
    "strings"
)

func req(u string, foundCm func(string)) string {
    resp, err := http.Get(u)
    if err != nil {
        fmt.Println(err) 
        return ""
    }
    defer resp.Body.Close()
    for p := xml.NewDecoder(resp.Body); ; {
        t, err := p.RawToken()
        switch s, ok := t.(xml.StartElement); {
        case err == io.EOF:
            return ""
        case err != nil:
            fmt.Println(err)
            return ""
        case !ok:
            continue
        case s.Name.Local == "cm":
            for _, a := range s.Attr {
                if a.Name.Local == "title" {
                    foundCm(a.Value)
                }
            }
        case s.Name.Local == "categorymembers" && len(s.Attr) > 0 &&
            s.Attr[0].Name.Local == "cmcontinue":
            return url.QueryEscape(s.Attr[0].Value)
        }
    }
    return ""
}

func main() {
    taskQuery := "http:
        "&format=xml&list=categorymembers&cmlimit=500" +
        "&cmtitle=Category:Programming_Tasks"
    continueAt := req(taskQuery, count)
    for continueAt > "" {
        continueAt = req(taskQuery+"&cmcontinue="+continueAt, count)
    }
    fmt.Printf("Total: %d examples.\n", total)
}

var marker = []byte("=={{header|")
var total int

func count(cm string) {
    taskFmt := "http:
    taskEsc := url.QueryEscape(strings.Replace(cm, " ", "_", -1))
    resp, err := http.Get(fmt.Sprintf(taskFmt, taskEsc))
    var page []byte
    if err == nil {
        page, err = ioutil.ReadAll(resp.Body)
        resp.Body.Close()
    }
    if err != nil {
        fmt.Println(err)
        return
    }
    examples := bytes.Count(page, marker)
    fmt.Printf("%s: %d\n", cm, examples)
    total += examples
}