-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstring.go
49 lines (46 loc) · 959 Bytes
/
string.go
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
45
46
47
48
49
package fiputil
import (
"fmt"
"strconv"
)
//ToString convert an arbitrary value to string.
//If the type of the value implements the `fmt.Stringer` interface, the `String()` method that is,
//this function will return as the `String()` returns.
func ToString(a interface{}) string {
switch a := a.(type) {
case string:
return a
case bool:
if a {
return "true"
} else {
return "false"
}
case int:
return strconv.Itoa(a)
case int8:
return strconv.Itoa(int(a))
case int16:
return strconv.Itoa(int(a))
case int32:
return strconv.Itoa(int(a))
case int64:
return strconv.FormatInt(a, 10)
case float32:
return strconv.FormatFloat(float64(a), 'f', -1, 32)
case float64:
return strconv.FormatFloat(float64(a), 'f', -1, 64)
case complex64:
//todo
case complex128:
//todo
case fmt.Stringer:
return a.String()
case struct{}:
return "[sruct]"
case interface{}:
return "[interface]"
}
fmt.Println()
return ""
}