Skip to content

Instantly share code, notes, and snippets.

@taji-taji
Last active September 5, 2015 08:05
Show Gist options
  • Save taji-taji/366786225ab80e429016 to your computer and use it in GitHub Desktop.
Save taji-taji/366786225ab80e429016 to your computer and use it in GitHub Desktop.
【Go】Goのsort.SearchInts()はPHPのin_array()とは全然違った! ref: http://qiita.com/taji-taji/items/ce0ac16adb880fd807b5
package main
import (
"fmt"
"sort"
)
func main() {
a := []int{1,9,3,7,5}
// まずソートする
sort.Ints(a)
fmt.Printf("%v\n", a) // [1 3 5 7 9]
// インデックス3が返ってくるはず...
b := sort.SearchInts(a, 7)
fmt.Printf("%v\n", b) // 3
// スライスの中にないことが分かるような値が返ってきてほしい...
c := sort.SearchInts(a, 4)
fmt.Printf("%v\n", c) // 2
}
c := sort.SearchInts(a, 4)
fmt.Printf("%v\n", c) // 2
func SearchStrings(a []string, x string) int
c := sort.SearchInts(a, 4)
fmt.Printf("%v\n", c) // 2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment