GithubHelp home page GithubHelp logo

2018-codemash-go's People

Contributors

zevdg avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

2018-codemash-go's Issues

How to avoid globals in HandleFunc?

I really enjoyed your workshop today. If you have the opportunity to post the solutions to the exercises we didn't get to finish in class, I would really appreciate it.

I had a slightly different approach to solving the exercises, and I was able to avoid globals until the very last exercise when I had to hoist the flags out of the main scope for the http.HandleFunc. This sort of bugged me, so I ended up put the handler logic into a closure, and close over the variables I wanted to use:

package main

import (
	"fmt"
	"math/rand"
	"time"
	"strings"
	"flag"
	"os"
	"log"
	"net/http"
	"github.com/zevdg/2018-codemash-go/labs/01/wordbank"
)

func ipsumHandler(wordbankFile string, words int, sentenceLength int) http.Handler {
	fn := func(w http.ResponseWriter, r *http.Request) {
		ipsum, err := generateIpsum(wordbankFile, words, sentenceLength)
		if err != nil {
			log.Fatal(err)
			panic(err)
		}
		fmt.Fprintf(w, ipsum)
	}
	return http.HandlerFunc(fn)
}

func main() {
	var words int
	flag.IntVar(&words, "words", 100, "an int")

	var sentenceLength int
	flag.IntVar(&sentenceLength, "sentence-length", 6, "an int")

	var wordbankFile string
	flag.StringVar(&wordbankFile, "wordbank", "nope", "File path")

	flag.Parse()

	rand.Seed(time.Now().UnixNano())

	ih := ipsumHandler(wordbankFile, words, sentenceLength)

	http.HandleFunc("/ipsum", ih)
	http.ListenAndServe(":12345", nil)
}

func generateIpsum(wordbankFile string, wordCount int, sentenceLength int) (string, error) {
	file, err := os.Open(wordbankFile)
	if err != nil {
		return "", fmt.Errorf("calculation failed: %s", err)
	}
	defer file.Close()

	wb, err := wordbank.NewWordBank(file)

	if err != nil {
		return "", fmt.Errorf("calculation failed: %s", err)
	}
	var s []string
	ipsum := make(chan string)
	go func() {
		for i := 0; i < wordCount/sentenceLength; i++ {

			ipsum <- generateSentence(wb, sentenceLength)
		}

		if wordCount%sentenceLength > 0 {
			ipsum <- generateSentence(wb, wordCount%sentenceLength)
		}
		close(ipsum)
	}()

	for elem := range ipsum {
		s = append(s, elem)
	}
	return strings.Join(s, ". ") + ".", nil
}

func generateSentence(wb *wordbank.WordBank, wordCount int) string {
	var s []string
	for i := 0; i < wordCount; i++ {
		s = append(s, wb.GetWord())
	}
	return strings.Join(s, " ")
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.