-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring.go
More file actions
44 lines (40 loc) · 915 Bytes
/
string.go
File metadata and controls
44 lines (40 loc) · 915 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// Package guti contains packages
package guti
import (
"fmt"
"sort"
"strconv"
)
// SortStrings A function that can sort a slice of strings in ascending or descending order.
func SortStrings(slice []string, ascending bool) []string {
if ascending {
sort.Strings(slice)
} else {
sort.Sort(sort.Reverse(sort.StringSlice(slice)))
}
return slice
}
// StringInSlice A function that can check if a given string exists in a slice of strings.
func StringInSlice(s string, slice []string) bool {
for _, item := range slice {
if item == s {
return true
}
}
return false
}
// ToString A function that can convert any data type to a string.
func ToString(i interface{}) string {
switch v := i.(type) {
case string:
return v
case []byte:
return string(v)
case int:
return strconv.Itoa(v)
case float64:
return strconv.FormatFloat(v, 'f', -1, 64)
default:
return fmt.Sprintf("%v", v)
}
}