mirror of
https://github.com/idanoo/GoCatFacts
synced 2025-07-22 16:29:11 +00:00
Update randomness
This commit is contained in:
parent
748270c22a
commit
b606317241
2 changed files with 15 additions and 4 deletions
4
go.sum
Normal file
4
go.sum
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
github.com/lukechampine/randmap v0.0.0-20161125183226-9e3c222d0413 h1:tysX0ocX3VYPAW06M8E5sEkRzr+7ygS162QpIi/N3hI=
|
||||||
|
github.com/lukechampine/randmap v0.0.0-20161125183226-9e3c222d0413/go.mod h1:CDBUzfMMkesqPDGmuMzuDrzBd2069GPe4wMJ3FC2sEw=
|
||||||
|
github.com/minio/blake2b-simd v0.0.0-20160723061019-3f5f724cb5b1 h1:lYpkrQH5ajf0OXOcUbGjvZxxijuBwbbmlSxLiuofa+g=
|
||||||
|
github.com/minio/blake2b-simd v0.0.0-20160723061019-3f5f724cb5b1/go.mod h1:pD8RvIylQ358TN4wwqatJ8rNavkEINozVn9DtGI3dfQ=
|
15
main.go
15
main.go
|
@ -2,16 +2,17 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
|
"crypto/rand"
|
||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"math/rand"
|
"math/big"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
port = "8080"
|
port = "8080"
|
||||||
facts = []string{}
|
facts = map[int64]string{}
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
@ -30,6 +31,7 @@ func main() {
|
||||||
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
||||||
io.WriteString(w, getRandomFact()+"\n")
|
io.WriteString(w, getRandomFact()+"\n")
|
||||||
})
|
})
|
||||||
|
|
||||||
err = http.ListenAndServe(":"+port, nil)
|
err = http.ListenAndServe(":"+port, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Error loading facts: %v", err)
|
log.Fatalf("Error loading facts: %v", err)
|
||||||
|
@ -45,10 +47,13 @@ func loadFacts() error {
|
||||||
defer file.Close()
|
defer file.Close()
|
||||||
|
|
||||||
scanner := bufio.NewScanner(file)
|
scanner := bufio.NewScanner(file)
|
||||||
|
i := 0
|
||||||
for scanner.Scan() {
|
for scanner.Scan() {
|
||||||
facts = append(facts, scanner.Text())
|
facts[int64(i)] = scanner.Text()
|
||||||
|
i++
|
||||||
}
|
}
|
||||||
|
|
||||||
|
log.Println("Loaded", len(facts), "cat facts")
|
||||||
return scanner.Err()
|
return scanner.Err()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -58,5 +63,7 @@ func getRandomFact() string {
|
||||||
return "No facts available."
|
return "No facts available."
|
||||||
}
|
}
|
||||||
|
|
||||||
return facts[rand.Intn(len(facts))]
|
counter := int64(len(facts) - 1)
|
||||||
|
n, _ := rand.Int(rand.Reader, big.NewInt(counter))
|
||||||
|
return facts[n.Int64()]
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue