Skip to content

Instantly share code, notes, and snippets.

@fsouza
Last active December 16, 2015 07:29
Show Gist options
  • Save fsouza/5399474 to your computer and use it in GitHub Desktop.
Save fsouza/5399474 to your computer and use it in GitHub Desktop.
func main() {
index, err := search.NewIndex(os.Args[1:]...)
if err != nil {
log.Fatal(err)
}
scanner := bufio.NewScanner(os.Stdin)
scanner.Split(bufio.ScanLines)
fmt.Print("> ")
for scanner.Scan() {
articles, err := index.Search(scanner.Text())
if err != nil {
fmt.Println(err)
} else {
fmt.Printf("%s.\n", strings.Join(articles, ", "))
}
fmt.Print("> ")
}
fmt.Println()
}
package main
import (
"fmt"
)
func main() {
var values []int
values = []int{10, 15, 18}
values = values[:2]
for i := 0; i < len(values); i++ {
fmt.Printf("values[%d]: %d.\n", i, values[i])
}
for i, value := range values {
fmt.Printf("values[%d]: %d.\n", i, value)
}
values = make([]int, 3)
values[0] = 10
values[1] = 15
values[2] = 18
values = append(values, 20, 30, 40, 50)
for i, value := range values {
fmt.Printf("values[%d]: %d.\n", i, value)
}
var array [16]int
values = array[:]
for i, value := range array {
fmt.Printf("array[%d]: %d.\n", i, value)
}
var count map[string]int
count = make(map[string]int)
count["x"] = 1
count["y"] = 1
count["x"] += 1
for key, value := range count {
fmt.Printf("count[%q]: %d.\n", key, value)
}
values = make([]int, 0, 6)
values = append(values, 10, 2, 4, 55, 82, 3)
values = append(values, 2)
}
package str
import (
"testing"
)
func TestMyStringReader(t *testing.T) {
buf := make([]byte, 8)
s := MyString("Francisco")
n, err := s.Read(buf)
if err != nil {
t.Fatal(err)
}
if n != len(buf) {
t.Errorf("MyString.Read() returned wrong int. Want %d. Got %d.", len(buf), n)
}
if string(buf) != string(s[:8]) {
t.Errorf("MyString.Read() wrote wrong content. Want %q. Got %q.", s[:8], string(buf))
}
buf = make([]byte, 8)
s = MyString("Chico")
n, err = s.Read(buf)
if err != nil {
t.Fatal(err)
}
if n != len(s) {
t.Errorf("MyString.Read() returned wrong int. Want %d. Got %d.", len(s), n)
}
if string(buf[:n]) != string(s) {
t.Errorf("MyString.Read() wrote wrong content. Want %q. Got %q.", s, string(buf))
}
}
func TestReverse(t *testing.T) {
var tests = []struct {
input string
want string
}{
{"Francisco", "ocsicnarF"},
{"ovo", "ovo"},
{"a", "a"},
{"soma", "amos"},
}
for _, tt := range tests {
got := Reverse(tt.input)
if got != tt.want {
t.Errorf("Reverse(%q): Want %q. Got %q.", tt.input, tt.want, got)
}
}
}
func BenchmarkReverse(b *testing.B) {
b.StopTimer()
inputs := []string{
"abjure", "abrogate", "abstemious", "acumen", "antebellum",
"auspicious", "belie", "bellicose", "bowdlerize", "chicanery",
"chromosome", "churlish", "circumlocution", "circumnavigate",
"deciduous", "deleterious", "diffident", "enervate", "enfranchise",
"epiphany", "equinox", "euro", "evanescent", "expurgate", "facetious",
"fatuous", "feckless", "fiduciary", "filibuster", "gamete", "gauche",
"gerrymander", "hegemony", "hemoglobin", "homogeneous", "hubris",
"hypotenuse", "impeach", "incognito", "incontrovertible", "inculcate",
"infrastructure", "interpolate", "irony", "jejune", "kinetic",
"kowtow", "laissez faire", "lexicon", "loquacious",
}
b.StartTimer()
for i := 0; i < b.N; i++ {
Reverse(inputs[i%len(inputs)])
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment