- Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathhelper.go
62 lines (55 loc) · 977 Bytes
/
helper.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
51
52
53
54
55
56
57
58
59
60
61
62
package utils
import (
"bufio"
"os"
"path/filepath"
"strconv"
)
const (
File_Name="../utils/intarray.txt"
Num_Vol=100000
)
//获取文件绝对路径
funcabsPath() string {
_fpath, err:=filepath.Abs(File_Name)
iferr!=nil {
panic(err)
}
return_fpath
}
//判断随机数文件是否存在
funcpathExists() bool {
_fpath:=absPath()
_, err:=os.Stat(_fpath)
ifos.IsNotExist(err) {
returnfalse
}
returntrue
}
//生成随机数文件
funcgen() {
_fpath:=absPath()
GenRandFile(Num_Vol, _fpath)
}
//获取随机数
funcGetArrayOfSize(nint) []int {
_fpath:=absPath()
//判断文件是否存在
if!pathExists() {
gen() //不存在创建
}
//存在读取
f, err:=os.Open(_fpath)
iferr!=nil {
panic(err)
}
deferf.Close()
numbers:=make([]int, n)
scanner:=bufio.NewScanner(f)
fori:=0; i<n; i++ {
scanner.Scan()
s, _:=strconv.Atoi(scanner.Text())
numbers=append(numbers, s)
}
returnnumbers
}