Skip to content

Instantly share code, notes, and snippets.

@pauladam
Last active August 29, 2015 14:23
Show Gist options
  • Select an option

  • Save pauladam/f41bb2320b5fdd9180fb to your computer and use it in GitHub Desktop.

Select an option

Save pauladam/f41bb2320b5fdd9180fb to your computer and use it in GitHub Desktop.
Scribe
// tree
// .
// ├── bin
// │   └── scribe
// └── src
// └── scribe
// └── scribe.go
// $ gb build all; bin/scribe
// Jesse
package main
import "os"
import "fmt"
import "time"
import "bufio"
import "math/rand"
func main() {
var names []string
inFile, _ := os.Open("names")
defer inFile.Close()
scanner := bufio.NewScanner(inFile)
scanner.Split(bufio.ScanLines)
for scanner.Scan() { names = append(names, scanner.Text()) }
rand.Seed(time.Now().UTC().UnixNano())
fmt.Println(names[rand.Intn(len(names))])
}
@mmaelzer
Copy link
Copy Markdown

you can simplify the import

import (
  "os"
  "fmt"
  "time"
  "bufio"
  "math/rand"
)

@mmaelzer
Copy link
Copy Markdown

to be idiomatic (but kinda silly for this use case) you might want to check the err return from os.Open() and maybe panic. Also, newline inside a for block 😃

@pauladam
Copy link
Copy Markdown
Author

thanks that all makes sense

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