如何使用文件名创建切片?

huangapple go评论71阅读模式
英文:

How to create slice with filenames

问题

有一个程序每秒钟创建一个文件。我想将文件名添加到切片中并打印它们。现在我的程序执行不正确,它只附加了一个文件名。所以我期望得到[]string{"1","2","3"},但实际上得到的是[]string{"1","1","1"}[]string{"2","2","2"}[]string{"3","3","3"}。如何修改我的程序以获得期望的结果?

package main

import (
	"encoding/csv"
	"fmt"
	"os"
	"strconv"
	"time"
)

func main() {
	for {
		time.Sleep(1 * time.Second)
		createFile()
	}
}

func createFile() {
	rowFile := time.Now().Second()
	fileName := strconv.Itoa(rowFile)
	file, err := os.OpenFile(fileName, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644)
	if err != nil {
		fmt.Println(err)
	}
	defer file.Close()

	writer := csv.NewWriter(file)
	writer.Comma = '|'

	err = writer.Write([]string{""})
	if err != nil {
		fmt.Println(err)
	}
	countFiles(fileName)
}

func countFiles(fileName string) {
	arrFiles := make([]string, 0, 3)
	for i := 0; i < 3; i++ {
		arrFiles = append(arrFiles, fileName)
	}
	fmt.Println(arrFiles) // 这里我期望得到 ["1","2","3"],然后 ["4","5","6"],依此类推。但现在得到的是 ["1","1","1"],然后 ["2","2","2"],依此类推
}

以上是你提供的代码的中文翻译。

英文:

There is a program which creates file per second. I want to append file names into slice and print them. Now my program executes incorrect, it appends names but only for one file name. So I expect to get []string{&quot;1&quot;,&quot;2&quot;,&quot;3&quot;}, instead I get []string{&quot;1&quot;,&quot;1&quot;,&quot;1&quot;}, []string{&quot;2&quot;,&quot;2&quot;,&quot;2&quot;}, []string{&quot;3&quot;,&quot;3&quot;,&quot;3&quot;}. How to correct my prog to get expected result?

package main

import (
	&quot;encoding/csv&quot;
	&quot;fmt&quot;
	&quot;os&quot;
	&quot;strconv&quot;
	&quot;time&quot;
)

func main() {
	for {
		time.Sleep(1 * time.Second)
		createFile()
	}
}

func createFile() {
	rowFile := time.Now().Second()
	fileName := strconv.Itoa(rowFile)
	file, err := os.OpenFile(fileName, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644)
	if err != nil {
		fmt.Println(err)
	}
	defer file.Close()

	writer := csv.NewWriter(file)
	writer.Comma = &#39;|&#39;

	err = writer.Write([]string{&quot;&quot;})
	if err != nil {
		fmt.Println(err)
	}
	countFiles(fileName)
}

func countFiles(fileName string) {
	arrFiles := make([]string, 0, 3)
	for i := 0; i &lt; 3; i++ {
		arrFiles = append(arrFiles, fileName)
	}
	fmt.Println(arrFiles)// here I expect [&quot;1&quot;,&quot;2&quot;,&quot;3&quot;] then [&quot;4&quot;,&quot;5&quot;,&quot;6&quot;] and so on. But now there is [&quot;1&quot;,&quot;1&quot;,&quot;1&quot;] then [&quot;2&quot;,&quot;2&quot;,&quot;2&quot;] and so on
}

答案1

得分: 2

createFile()函数不以任何方式保存已创建的文件名。你可以像这样做:

package main

import (
	"encoding/csv"
	"fmt"
	"os"
	"strconv"
	"time"
)

func main() {
	files := []string{}
	for {
		time.Sleep(1 * time.Second)
		files = append(files, createFile())
		fmt.Println(files)
	}
}

func createFile() string {
	rowFile := time.Now().Second()
	fileName := strconv.Itoa(rowFile)

	file, err := os.OpenFile(fileName, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644)
	if err != nil {
		fmt.Println(err)
	}
	defer file.Close()

	writer := csv.NewWriter(file)
	writer.Comma = '|'

	err = writer.Write([]string{""})
	if err != nil {
		fmt.Println(err)
	}
	return fileName
}

请注意,这只是一个示例代码,用于演示如何创建文件并将其添加到文件列表中。文件名是使用当前时间的秒数生成的,并且文件以CSV格式写入一个空字符串。

英文:

createFile() does not persist created file names in any way. You can do something like that:

 package main

import (
	&quot;encoding/csv&quot;
	&quot;fmt&quot;
	&quot;os&quot;
	&quot;strconv&quot;
	&quot;time&quot;
)

func main() {
	files := []string{}
	for {
		time.Sleep(1 * time.Second)
		files = append(files, createFile())
		fmt.Println(files)
	}
}

func createFile() string {
	rowFile := time.Now().Second()
	fileName := strconv.Itoa(rowFile)

	file, err := os.OpenFile(fileName, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644)
	if err != nil {
		fmt.Println(err)
	}
	defer file.Close()

	writer := csv.NewWriter(file)
	writer.Comma = &#39;|&#39;

	err = writer.Write([]string{&quot;&quot;})
	if err != nil {
		fmt.Println(err)
	}
	return fileName
}

huangapple
  • 本文由 发表于 2022年9月13日 04:03:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/73694707.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定