generated from Gophing-Around/hashcode-golang-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
50 lines (40 loc) · 886 Bytes
/
utils.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
50
package main
import (
"io/ioutil"
"strconv"
"strings"
)
func readFile(source string) string {
in, err := ioutil.ReadFile(source)
if err != nil {
panic(err)
}
return string(in)
}
func toint(num string) int {
res, err := strconv.Atoi(num)
if err != nil {
panic(err)
}
return res
}
func splitNewLines(rawContent string) []string {
return strings.Split(rawContent, "\n")
}
func splitSpaces(lineContent string) []string {
return strings.Split(lineContent, " ")
}
func joinWithSpaces(partials []string) string {
return strings.Join(partials, " ")
}
/* useful pieces of code:
***** [1] sorting *****
sort.Slice(arrayOfValues, func(i, j int) bool {
valueA := arrayOfValues[i]
valueB := arrayOfValues[j]
return valueA.score > valueB.score
})
***** [2] Format output string with Sprintf *****
output := ""
output += fmt.Sprintf("%s %d\n", stringValue, intValue)
*/