Skip to content

Commit

Permalink
feat(11-select): 🎉 11-select initial framework without goroutines
Browse files Browse the repository at this point in the history
  • Loading branch information
sheldonhull committed Sep 7, 2021
1 parent 9fead70 commit b0a2641
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/11-select/go.mod
@@ -0,0 +1,5 @@
module racer

go 1.17

require github.com/matryer/is v1.4.0
2 changes: 2 additions & 0 deletions src/11-select/go.sum
@@ -0,0 +1,2 @@
github.com/matryer/is v1.4.0 h1:sosSmIWwkYITGrxZ25ULNDeKiMNzFSr4V/eqBQP0PeE=
github.com/matryer/is v1.4.0/go.mod h1:8I/i5uYgLzgsgEloJE1U6xx5HkBQpAZvepWuujKwMRU=
22 changes: 22 additions & 0 deletions src/11-select/racer.go
@@ -0,0 +1,22 @@
package racer

import (
http "net/http"
"time"
)

// Racer returns the fastest responding website.
func Racer(a, b string) (winner string) {
startA := time.Now()
http.Get(a)
aDuration := time.Since(startA)

startB := time.Now()
http.Get(b)
bDuration := time.Since(startB)

if aDuration < bDuration {
return a
}
return b
}
20 changes: 20 additions & 0 deletions src/11-select/racer_test.go
@@ -0,0 +1,20 @@
package racer_test

import (
src "racer"
"testing"

iz "github.com/matryer/is"
)

func TestRacer(t *testing.T) {
is := iz.New(t)

slowURL := "https://www.facebook.com"
fastURL := "https://www.quii.co.uk"

want := fastURL
got := src.Racer(slowURL, fastURL)

is.Equal(got, want) // the fast URL should be quii obviously
}

0 comments on commit b0a2641

Please sign in to comment.