2018-codemash-go's People
Forkers
pstanos jcoulter aporlando apasternack aqualad cpbyrne mpreston88 jpierson honestraccoon mkasek stevenacoffman devglen augz singram2018-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
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google โค๏ธ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.