快速导入本地文本字典至字符组
```go
package main
import (
"os"
"log"
"fmt"
"bufio"
"reflect"
)
func main() {
newArray,_ := fileToArray("dict.txt")
fmt.Println(reflect.TypeOf(newArray))
fmt.Println(newArray[0:3])
}
func fileToArray(dict string) ([]string,error){
file, err := os.Open(dict)
if err != nil {
log.Printf("Cannot open text file: %s, err: [%v]", dict, err)
return nil,err
}
defer file.Close()
scanner := bufio.NewScanner(file)
var line string
var tmp []string
for scanner.Scan() {
line = scanner.Text()
//添加进组
tmp = append(tmp,line)
//do_your_function(line)
}
if err := scanner.Err(); err != nil {
log.Printf("Cannot scanner text file: %s, err: [%v]", dict, err)
return nil,err
}
return tmp,err
}
```
有疑问加站长微信联系(非本文作者))