The <code>sort</code> package includes the necessary methods to allow sorting of
arrays of integers, strings, etc.; here's the code for arrays of <code>int</code>
<p>
-<pre> <!-- progs/sort.go /type.*IntArray/ /Swap/ -->
-33 type IntArray []int
+<pre> <!-- progs/sort.go /type.*IntSlice/ /Swap/ -->
+33 type IntSlice []int
-35 func (p IntArray) Len() int { return len(p) }
-36 func (p IntArray) Less(i, j int) bool { return p[i] < p[j] }
-37 func (p IntArray) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
+35 func (p IntSlice) Len() int { return len(p) }
+36 func (p IntSlice) Less(i, j int) bool { return p[i] < p[j] }
+37 func (p IntSlice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
</pre>
<p>
Here we see methods defined for non-<code>struct</code> types. You can define methods
<pre> <!-- progs/sortmain.go /func.ints/ /^}/ -->
12 func ints() {
13 data := []int{74, 59, 238, -784, 9845, 959, 905, 0, 0, 42, 7586, -5467984, 7586}
-14 a := sort.IntArray(data)
+14 a := sort.IntSlice(data)
15 sort.Sort(a)
16 if !sort.IsSorted(a) {
17 panic("fail")
The "sort" package includes the necessary methods to allow sorting of
arrays of integers, strings, etc.; here's the code for arrays of "int"
---PROG progs/sort.go /type.*IntArray/ /Swap/
+--PROG progs/sort.go /type.*IntSlice/ /Swap/
Here we see methods defined for non-"struct" types. You can define methods
for any type you define and name in your package.
// Convenience types for common cases
-type IntArray []int
+type IntSlice []int
-func (p IntArray) Len() int { return len(p) }
-func (p IntArray) Less(i, j int) bool { return p[i] < p[j] }
-func (p IntArray) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
+func (p IntSlice) Len() int { return len(p) }
+func (p IntSlice) Less(i, j int) bool { return p[i] < p[j] }
+func (p IntSlice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
-type Float64Array []float64
+type Float64Slice []float64
-func (p Float64Array) Len() int { return len(p) }
-func (p Float64Array) Less(i, j int) bool { return p[i] < p[j] }
-func (p Float64Array) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
+func (p Float64Slice) Len() int { return len(p) }
+func (p Float64Slice) Less(i, j int) bool { return p[i] < p[j] }
+func (p Float64Slice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
-type StringArray []string
+type StringSlice []string
-func (p StringArray) Len() int { return len(p) }
-func (p StringArray) Less(i, j int) bool { return p[i] < p[j] }
-func (p StringArray) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
+func (p StringSlice) Len() int { return len(p) }
+func (p StringSlice) Less(i, j int) bool { return p[i] < p[j] }
+func (p StringSlice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
// Convenience wrappers for common cases
-func SortInts(a []int) { Sort(IntArray(a)) }
-func SortFloat64s(a []float64) { Sort(Float64Array(a)) }
-func SortStrings(a []string) { Sort(StringArray(a)) }
+func SortInts(a []int) { Sort(IntSlice(a)) }
+func SortFloat64s(a []float64) { Sort(Float64Slice(a)) }
+func SortStrings(a []string) { Sort(StringSlice(a)) }
-func IntsAreSorted(a []int) bool { return IsSorted(IntArray(a)) }
-func Float64sAreSorted(a []float64) bool { return IsSorted(Float64Array(a)) }
-func StringsAreSorted(a []string) bool { return IsSorted(StringArray(a)) }
+func IntsAreSorted(a []int) bool { return IsSorted(IntSlice(a)) }
+func Float64sAreSorted(a []float64) bool { return IsSorted(Float64Slice(a)) }
+func StringsAreSorted(a []string) bool { return IsSorted(StringSlice(a)) }
func ints() {
data := []int{74, 59, 238, -784, 9845, 959, 905, 0, 0, 42, 7586, -5467984, 7586}
- a := sort.IntArray(data)
+ a := sort.IntSlice(data)
sort.Sort(a)
if !sort.IsSorted(a) {
panic("fail")
func strings() {
data := []string{"monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday"}
- a := sort.StringArray(data)
+ a := sort.StringSlice(data)
sort.Sort(a)
if !sort.IsSorted(a) {
panic("fail")
// sortFlags returns the flags as a slice in lexicographical sorted order.
func sortFlags(flags map[string]*Flag) []*Flag {
- list := make(sort.StringArray, len(flags))
+ list := make(sort.StringSlice, len(flags))
i := 0
for _, f := range flags {
list[i] = f.Name
// Search returns the result of applying SearchInts to the receiver and x.
-func (p IntArray) Search(x int) int { return SearchInts(p, x) }
+func (p IntSlice) Search(x int) int { return SearchInts(p, x) }
// Search returns the result of applying SearchFloat64s to the receiver and x.
// Search returns the result of applying SearchStrings to the receiver and x.
-func (p StringArray) Search(x string) int { return SearchStrings(p, x) }
+func (p StringSlice) Search(x string) int { return SearchStrings(p, x) }
{"SearchInts", SearchInts(data, 11), 8},
{"SearchFloat64s", SearchFloat64s(fdata, 2.1), 4},
{"SearchStrings", SearchStrings(sdata, ""), 0},
- {"IntArray.Search", IntArray(data).Search(0), 2},
+ {"IntSlice.Search", IntSlice(data).Search(0), 2},
{"Float64Array.Search", Float64Array(fdata).Search(2.0), 3},
- {"StringArray.Search", StringArray(sdata).Search("x"), 3},
+ {"StringSlice.Search", StringSlice(sdata).Search("x"), 3},
}
// Convenience types for common cases
-// IntArray attaches the methods of Interface to []int, sorting in increasing order.
-type IntArray []int
+// IntSlice attaches the methods of Interface to []int, sorting in increasing order.
+type IntSlice []int
-func (p IntArray) Len() int { return len(p) }
-func (p IntArray) Less(i, j int) bool { return p[i] < p[j] }
-func (p IntArray) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
+func (p IntSlice) Len() int { return len(p) }
+func (p IntSlice) Less(i, j int) bool { return p[i] < p[j] }
+func (p IntSlice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
// Sort is a convenience method.
-func (p IntArray) Sort() { Sort(p) }
+func (p IntSlice) Sort() { Sort(p) }
// Float64Array attaches the methods of Interface to []float64, sorting in increasing order.
func (p Float64Array) Sort() { Sort(p) }
-// StringArray attaches the methods of Interface to []string, sorting in increasing order.
-type StringArray []string
+// StringSlice attaches the methods of Interface to []string, sorting in increasing order.
+type StringSlice []string
-func (p StringArray) Len() int { return len(p) }
-func (p StringArray) Less(i, j int) bool { return p[i] < p[j] }
-func (p StringArray) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
+func (p StringSlice) Len() int { return len(p) }
+func (p StringSlice) Less(i, j int) bool { return p[i] < p[j] }
+func (p StringSlice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
// Sort is a convenience method.
-func (p StringArray) Sort() { Sort(p) }
+func (p StringSlice) Sort() { Sort(p) }
// Convenience wrappers for common cases
// SortInts sorts an array of ints in increasing order.
-func SortInts(a []int) { Sort(IntArray(a)) }
+func SortInts(a []int) { Sort(IntSlice(a)) }
// SortFloat64s sorts an array of float64s in increasing order.
func SortFloat64s(a []float64) { Sort(Float64Array(a)) }
// SortStrings sorts an array of strings in increasing order.
-func SortStrings(a []string) { Sort(StringArray(a)) }
+func SortStrings(a []string) { Sort(StringSlice(a)) }
// IntsAreSorted tests whether an array of ints is sorted in increasing order.
-func IntsAreSorted(a []int) bool { return IsSorted(IntArray(a)) }
+func IntsAreSorted(a []int) bool { return IsSorted(IntSlice(a)) }
// Float64sAreSorted tests whether an array of float64s is sorted in increasing order.
func Float64sAreSorted(a []float64) bool { return IsSorted(Float64Array(a)) }
// StringsAreSorted tests whether an array of strings is sorted in increasing order.
-func StringsAreSorted(a []string) bool { return IsSorted(StringArray(a)) }
+func StringsAreSorted(a []string) bool { return IsSorted(StringSlice(a)) }
var float64s = [...]float64{74.3, 59.0, 238.2, -784.0, 2.3, 9845.768, -959.7485, 905, 7.8, 7.8}
var strings = [...]string{"", "Hello", "foo", "bar", "foo", "f00", "%*&^*&^&", "***"}
-func TestSortIntArray(t *testing.T) {
+func TestSortIntSlice(t *testing.T) {
data := ints
- a := IntArray(data[0:])
+ a := IntSlice(data[0:])
Sort(a)
if !IsSorted(a) {
t.Errorf("sorted %v", ints)
}
}
-func TestSortStringArray(t *testing.T) {
+func TestSortStringSlice(t *testing.T) {
data := strings
- a := StringArray(data[0:])
+ a := StringSlice(data[0:])
Sort(a)
if !IsSorted(a) {
t.Errorf("sorted %v", strings)
// Convert array of string to array
// of NUL-terminated byte pointer.
-func StringArrayPtr(ss []string) []*byte {
+func StringSlicePtr(ss []string) []*byte {
bb := make([]*byte, len(ss)+1)
for i := 0; i < len(ss); i++ {
bb[i] = StringBytePtr(ss[i])
// Convert args to C form.
argv0p := StringBytePtr(argv0)
- argvp := StringArrayPtr(argv)
+ argvp := StringSlicePtr(argv)
var chroot *byte
if attr.Chroot != "" {
_, _, e := Syscall(SYS_EXEC,
uintptr(unsafe.Pointer(StringBytePtr(argv0))),
- uintptr(unsafe.Pointer(&StringArrayPtr(argv)[0])),
+ uintptr(unsafe.Pointer(&StringSlicePtr(argv)[0])),
0)
return NewError(e)
// Convert array of string to array
// of NUL-terminated byte pointer.
-func StringArrayPtr(ss []string) []*byte {
+func StringSlicePtr(ss []string) []*byte {
bb := make([]*byte, len(ss)+1)
for i := 0; i < len(ss); i++ {
bb[i] = StringBytePtr(ss[i])
// Convert args to C form.
argv0p := StringBytePtr(argv0)
- argvp := StringArrayPtr(argv)
- envvp := StringArrayPtr(attr.Env)
+ argvp := StringSlicePtr(argv)
+ envvp := StringSlicePtr(attr.Env)
if OS == "freebsd" && len(argv[0]) > len(argv0) {
argvp[0] = argv0p
func Exec(argv0 string, argv []string, envv []string) (err int) {
_, _, err1 := RawSyscall(SYS_EXECVE,
uintptr(unsafe.Pointer(StringBytePtr(argv0))),
- uintptr(unsafe.Pointer(&StringArrayPtr(argv)[0])),
- uintptr(unsafe.Pointer(&StringArrayPtr(envv)[0])))
+ uintptr(unsafe.Pointer(&StringSlicePtr(argv)[0])),
+ uintptr(unsafe.Pointer(&StringSlicePtr(envv)[0])))
return int(err1)
}
fmt.Print("}\n\n")
}
- decl := make(sort.StringArray, len(list))
+ decl := make(sort.StringSlice, len(list))
ndecl := 0
for _, name := range list {
if _, ok := category[name]; !ok {
fmt.Print("}\n\n")
}
- decl := make(sort.StringArray, len(list))
+ decl := make(sort.StringSlice, len(list))
ndecl := 0
for _, name := range list {
if doProps {