我不能像我想的那样返回字符串切片。只通过最后一个
Posted
技术标签:
【中文标题】我不能像我想的那样返回字符串切片。只通过最后一个【英文标题】:I can't return string slice like I would like to. Passing just the last one 【发布时间】:2018-10-02 04:55:19 【问题描述】:我编写了这段代码来获取目录中文件的列表,将名称附加到一个切片中并一个一个打开它们,打开文件后,我在文件中搜索一些单词,如果找到,将它们写入新文件。 但是我总是在新文件中得到相同的词,我不知道为什么
package main
import (
"bufio"
"fmt"
"io/ioutil"
"log"
"os"
"strings"
"time"
)
const dir_to_read_path string = "path"
func main()
start := time.Now()
temp_string_filename := ""
temp_string_filename_counter := 0
//defer list_file()
// just pass the file name
for k := range list_file()
temp_string_filename = list_file()[temp_string_filename_counter]
if true
k = k
temp_string_filename_counter++
b, err := ioutil.ReadFile(temp_string_filename)
if err != nil
fmt.Print(err)
// convert content to a 'string'
str := string(b)
control_params := []string"numpy", "grabscreen", "cv2", "time", "os", "pandas", "tqdm", "collections", "models", "random", "inception_v3", "googlenet", "shuffle", "getkeys", "tflearn", "directkeys", "statistics", "motion", "tflearn.layers.conv", "conv_2d", "max_pool_2d", "avg_pool_2d", "conv_3d", "max_pool_3d", "avg_pool_3d"
temp_string_filename = dir_to_read_path + "output_" + temp_string_filename
fmt.Println("Writing file n. ", k)
file, err := os.Create(temp_string_filename)
if err != nil
log.Fatal("Cannot create file", err)
for _, z := range isValueInList(control_params, str, list_file())
fmt.Fprintf(file, z)
fmt.Fprintf(file, "\n")
defer file.Close()
elapsed := time.Since(start)
log.Printf("Execution took %s", elapsed)
func isValueInList(list []string, file_string string, read_file []string) []string
encountered_modules := make([]string, 0, 10)
temp_string_filename := ""
temp_string_filename_counter := 0
encountered := map[string]bool
result := make([]string, 0, 10)
final_result := [][]string
for z := range read_file
fmt.Println("Reading file n. ", z)
temp_string_filename = read_file[temp_string_filename_counter]
f, _ := os.Open(temp_string_filename)
defer f.Close()
scanner := bufio.NewScanner(f)
scanner.Split(bufio.ScanWords)
for scanner.Scan()
line := scanner.Text()
for _, v := range list
if v == line
encountered_modules = append(encountered_modules, line)
for v := range encountered_modules
if encountered[encountered_modules[v]] == true
// Do not add duplicate.
else
// Record this element as an encountered element.
encountered[encountered_modules[v]] = true
result = append(result, encountered_modules[v])
temp_string_filename_counter++
final_result = append(final_result, result)
return result
func list_file() []string
files_names := make([]string, 0, 10)
files, err := ioutil.ReadDir("./")
if err != nil
log.Fatal(err)
for _, f := range files
if strings.HasSuffix(f.Name(), ".txt")
files_names = append(files_names, string(f.Name()))
return files_names
【问题讨论】:
你为什么不遵循 go 标准语法。只是想知道。如果所有人都遵循类似的规则,世界会更容易。 我是 Go 新手,我需要一些关于上述问题的建议 【参考方案1】:很难确定,因为您的代码难以阅读,但这看起来特别可疑(在伪代码中),
// main
for each file in list_file()
result =
// isValueInList
var result
for each file in list_file()
for each word in file
if word in wordlist and not in result
result = append(result, word)
// all the words in wordlist in any of the files
return result
// main
write result
您的代码还有其他问题。
这是一个更易读的示例(初稿),说明您似乎正在尝试做的事情(Python 文件中的 Python 模块?):
package main
import (
"bufio"
"bytes"
"fmt"
"io/ioutil"
"os"
"path/filepath"
)
var modules = map[string]bool
"numpy": true, "grabscreen": true, "cv2": true, "time": true, "os": true, "pandas": true, "tqdm": true, "collections": true,
"models": true, "random": true, "inception_v3": true, "googlenet": true, "shuffle": true, "getkeys": true, "tflearn": true,
"directkeys": true, "statistics": true, "motion": true, "tflearn.layers.conv": true, "conv_2d": true,
"max_pool_2d": true, "avg_pool_2d": true, "conv_3d": true, "max_pool_3d": true, "avg_pool_3d": true,
func findWords(filename string, lexicon map[string]bool) error
f, err := os.Open(filename)
if err != nil
return err
defer f.Close()
words := make(map[string]bool)
s := bufio.NewScanner(f)
s.Split(bufio.ScanWords)
for s.Scan()
word := s.Text()
if _, exists := lexicon[word]; exists
words[word] = true
if s.Err(); err != nil
return err
var buf bytes.Buffer
for word := range words
buf.WriteString(word)
buf.WriteString("\n")
if buf.Len() > 0
err := ioutil.WriteFile(filename+`.words`, buf.Bytes(), 0666)
if err != nil
return err
return nil
func main()
dir := `./`
files, err := ioutil.ReadDir(dir)
if err != nil
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
for _, file := range files
filename := file.Name()
if filepath.Ext(filename) != ".py"
continue
findWords(filename, modules)
if err != nil
fmt.Fprintln(os.Stderr, err)
【讨论】:
【参考方案2】:你的代码有一些错误,所以我重写了大部分代码。 我做了什么 : 1) 打开一个文件 2)读一行 3)比较 4) 检查目标文件是否存在 5)如果没有,创建它 6)如果是,附加到它 7) 写信给它 8) 关闭目标文件 9) 如果有更多行,则转到 2 10) 如果有更多文件,则转到 1
我试图让它对每个人都具有可读性,以便每个人都能理解它。
package main
import (
"bufio"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"strconv"
"strings"
"time"
)
const readDir string = "./"
var startTime time.Time
func main()
for noFile, fileName := range listFile()
startTime = time.Now()
fileInput, err := os.Open(fileName)
if err != nil
log.Fatal(err)
defer fileInput.Close()
scanner := bufio.NewScanner(fileInput)
for scanner.Scan()
for _, targetContent := range []string"numpy", "grabscreen", "cv2", "time", "os", "pandas", "tqdm", "collections", "models", "random", "inception_v3", "googlenet", "shuffle", "getkeys", "tflearn", "directkeys", "statistics", "motion", "tflearn.layers.conv", "conv_2d", "max_pool_2d", "avg_pool_2d", "conv_3d", "max_pool_3d", "avg_pool_3d"
if strings.Contains(scanner.Text(), targetContent)
if _, err := os.Stat(readDir + "output_" + strconv.Itoa(noFile)); os.IsNotExist(err)
fmt.Println("File : " + readDir + "output_" + strconv.Itoa(noFile) + " does not exists, creating it now!")
createFile, err := os.Create(readDir + "output_" + strconv.Itoa(noFile))
if err != nil
panic(err)
createFile.Close()
fileOutput, err := os.OpenFile(readDir+"output_"+strconv.Itoa(noFile), os.O_APPEND|os.O_WRONLY, 0600)
if err != nil
panic(err)
if _, err = fileOutput.WriteString("contains : " + targetContent + " in : " + scanner.Text() + "\n"); err != nil
panic(err)
fileOutput.Close()
fmt.Println("Writing file : ", readDir+"output_"+strconv.Itoa(noFile))
fmt.Println("contains : " + targetContent + " in : " + scanner.Text())
if err := scanner.Err(); err != nil
log.Fatal(err)
log.Printf("Execution took %s", time.Since(startTime))
func listFile() []string
filesNames := make([]string, 0, 100)
files, err := ioutil.ReadDir(readDir)
if err != nil
log.Fatal(err)
for _, f := range files
if strings.HasSuffix(f.Name(), ".txt")
fileName, err := filepath.Abs(string(f.Name()))
if err != nil
log.Fatal(err)
filesNames = append(filesNames, fileName)
return filesNames
【讨论】:
以上是关于我不能像我想的那样返回字符串切片。只通过最后一个的主要内容,如果未能解决你的问题,请参考以下文章