Skip to content

Swarna's solutions for Quiz and Url shortener#4

Open
SwarnaLathaNatarajan wants to merge 6 commits into
SpectraLogic:masterfrom
SwarnaLathaNatarajan:master
Open

Swarna's solutions for Quiz and Url shortener#4
SwarnaLathaNatarajan wants to merge 6 commits into
SpectraLogic:masterfrom
SwarnaLathaNatarajan:master

Conversation

@SwarnaLathaNatarajan
Copy link
Copy Markdown

@SwarnaLathaNatarajan SwarnaLathaNatarajan commented May 21, 2020

Comment thread urlshort/handler.go
Comment on lines +61 to +62
key := entry["path"]
pathMap[key] = entry["url"]
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like you could have used a struct instead of a map here. Using a struct would be good practice for using go tags.

Comment thread urlshort/handler.go
return out, err
}

func buildMap(parsedYaml []map[string]string) map[string]string {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this going to only accept YAML? The bonus objective is to also be able to use JSON.

Comment thread urlshort/handler.go
Comment on lines +18 to +23
path, ok := pathsToUrls[r.URL.Path]
if ok {
http.Redirect(w, r, path, http.StatusFound)
} else {
fallback.ServeHTTP(w, r)
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In go you will typically return instead of using else statements where appropriate.

Suggested change
path, ok := pathsToUrls[r.URL.Path]
if ok {
http.Redirect(w, r, path, http.StatusFound)
} else {
fallback.ServeHTTP(w, r)
}
if path, ok := pathsToUrls[r.URL.Path]; ok {
http.Redirect(w, r, path, http.StatusFound)
}
fallback.ServeHTTP(w, r)

Comment thread urlshort/handler.go
return func(w http.ResponseWriter, r *http.Request) {
path, ok := pathsToUrls[r.URL.Path]
if ok {
http.Redirect(w, r, path, http.StatusFound)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is http.StatusFound correct? I remember needing to use a 211 or some other status to signify a redirect.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Checks out

Comment thread urlshort/handler.go

func parseYAML(yml []byte) (out []map[string]string, err error) {
err = yaml.Unmarshal(yml, &out)
return out, err
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using the named return values in the signature was a clever idea here. When you do that you don't need to specify the variables in the return statement (as long as you are returning the signature values).

Comment thread urlshort/handler.go
return MapHandler(pathMap, fallback), nil
}

func parseYAML(yml []byte) (out []map[string]string, err error) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure you need this function since it would only be two lines in the YAMLHandler function.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The parseYAML stub was suggested in the problem. That's why I went ahead with it.

Comment thread urlshort/handler.go
@@ -33,5 +42,24 @@ func MapHandler(pathsToUrls map[string]string, fallback http.Handler) http.Handl
// a mapping of paths to urls.
func YAMLHandler(yml []byte, fallback http.Handler) (http.HandlerFunc, error) {
// TODO: Implement this...
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you still need this TODO?

Comment thread urlshort/handler.go
@@ -12,7 +14,14 @@ import (
// http.Handler will be called instead.
func MapHandler(pathsToUrls map[string]string, fallback http.Handler) http.HandlerFunc {
// TODO: Implement this...
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you still need this TODO?

@SwarnaLathaNatarajan
Copy link
Copy Markdown
Author

@gonzogomez I've updated the quiz problem with flags and pointer receivers as you mentioned.

Comment thread quiz/main.go
}

func (q *Quiz) readProblems() {
csvfile, err := os.Open(q.filename)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should probably make sure you close the file after opening it.
defer csvfile.Close()

Comment thread quiz/main.go
Comment on lines +43 to +46
for i := 0; i < len(q.problems); i++ {
question, answer := q.problems[i][0], q.problems[i][1]
fmt.Println("What is " + question + " ?")
userAnswer, _ := in.ReadString('\n')
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In Go, you'll typically see people iterating through slices using the range clause.
i.e., your code might look something like:

Suggested change
for i := 0; i < len(q.problems); i++ {
question, answer := q.problems[i][0], q.problems[i][1]
fmt.Println("What is " + question + " ?")
userAnswer, _ := in.ReadString('\n')
for _, problem := range q.problems {
question, answer := problem[0], problem[1]
fmt.Printf("What is %s?\n", question)
userAnswer, _ := in.ReadString('\n')
// Rest of your code here
}

Also note the use of fmt.Printf() instead of fmt.Println(). It's not actually important in this case, given that question is just a string, but I thought I'd make a note just in case you weren't familiar.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh okay. I'll make sure that I follow this in the future.

Comment thread quiz/main.go
filename string
timeLimit int
shuffle bool
problems [][]string
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like a Problem or Question struct would be better than a slice of slices.

Comment thread quiz/main.go
q.problems = records
}
func (q *Quiz) shuffleProblems() {
rand.Seed(time.Now().UnixNano())
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seeding should only happen once and in the main function.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants