从命令行输出写入文件

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

Writing to file from cmd output

问题

我正在尝试用Go语言编写一段小代码,用于收集和保存来自IPFS的统计信息。
我的Go代码将执行IPFS命令,并将其输出保存在.txt文件中,并不断更新该.txt文件。
我在处理这个问题时遇到了困难。

这是我的代码:

package main

import (
	"fmt"
	"io"
	"log"
	"os"
	"os/exec"
	"time"
)

func ipfsCommand() (ipfsOutput string) {
	// output and error
	out, err := exec.Command("ipfs","stats","bitswap","--human").Output()
	// if there are errors, print/log them
	if err != nil {
		log.Printf("error!")
		log.Fatal(err)
	} else {
		log.Printf("no error, printing output")
		fmt.Printf("%s", out)
	}
	return
}

func writeToFile(message string) error {
	f, err := os.Create("outputTest2_2.txt")
	if err != nil {
		return err
	}
	defer f.Close()
	l, err := io.WriteString(f, message)
	if err != nil {
		fmt.Println(err)
		f.Close()
		return err
	}
	fmt.Println(l, "bytes written successfully")

	return f.Sync()
}

func main() {
	// get current time
	currentTime := time.Now()
	fmt.Println("YYYY.MM.DD : ", currentTime.Format("2006.01.02 15:04:05"))
	writeToFile(currentTime)
	// get output from ipfs command
	msg := ipfsCommand()
	// write the output to file
	writeToFile(msg)
	fmt.Println("file written!!!")
	
/*	// write to file many times
	for i:=0;i<3;i++{
		// get output from ipfs command
		msg := ipfsCommand()
		// write the output to file
		writeToFile(msg)
	}*/
}

当运行上述代码时,出现以下错误:

# command-line-arguments
.\test2.go:49:13: cannot use currentTime (type time.Time) as type string in argument to writeToFile

我想要从IPFS获取输出,并将其与当前时间一起保存到.txt文件中。我希望在循环中执行此操作,因为我想要在较长的时间内保存来自IPFS的输出。

英文:

I am trying to write a small code in Go that will collect and save stats from IPFS.
So my Go code will execute IPFS command and save its output in .txt file and keep updating that .txt file.
I am having trouble doing that.

This is my code:

package main
import (
&quot;fmt&quot;
&quot;io&quot;
&quot;log&quot;
&quot;os&quot;
&quot;os/exec&quot;
&quot;time&quot;
)
func ipfsCommand() (ipfsOutput string) {
// output and error
out, err := exec.Command(&quot;ipfs&quot;,&quot;stats&quot;,&quot;bitswap&quot;,&quot;--human&quot;).Output()
// if there are errors, print/log them
if err != nil {
log.Printf(&quot;error!&quot;)
log.Fatal(err)
} else {
log.Printf(&quot;no error, printing output&quot;)
fmt.Printf(&quot;%s&quot;, out)
}
return
}
func writeToFile(message string) error {
f, err := os.Create(&quot;outputTest2_2.txt&quot;)
if err != nil {
return err
}
defer f.Close()
l, err := io.WriteString(f, message)
if err != nil {
fmt.Println(err)
f.Close()
return err
}
fmt.Println(l, &quot;bytes written successfully&quot;)
return f.Sync()
}
func main() {
// get current time
currentTime := time.Now()
fmt.Println(&quot;YYYY.MM.DD : &quot;, currentTime.Format(&quot;2006.01.02 15:04:05&quot;))
writeToFile(currentTime)
// get output from ipfs command
msg := ipfsCommand()
// write the output to file
writeToFile(msg)
fmt.Println(&quot;file written!!!&quot;)
/*	// write to file many times
for i:=0;i&lt;3;i++{
// get output from ipfs command
msg := ipfsCommand()
// write the output to file
writeToFile(msg)
}*/
}

When the above code is run, this is the error:

# command-line-arguments
.\test2.go:49:13: cannot use currentTime (type time.Time) as type string in argument to writeToFile

Again, I want to get output from IPFS and save it to .txt file along with current time. I want to do this in loop because I want to save output from IPFS over a long period of time.

答案1

得分: 2

我尝试修复你的脚本,但它有太多问题。这是一个重写版本,也许你可以将其用作新的起点:

package main
import (
"os"
"os/exec"
"time"
)
func main() {
f, err := os.Create("outputTest2_2.txt")
if err != nil {
panic(err)
}
defer f.Close()
currentTime, err := time.Now().MarshalText()
if err != nil {
panic(err)
}
f.Write(append(currentTime, '\n'))
msg, err := exec.Command("go", "env").Output()
if err != nil {
panic(err)
}
f.Write(msg)
}
英文:

I tried to fix your script as is, but it just has too many issues. Here is a
rewrite, maybe you can use it as a new starting point:

package main
import (
&quot;os&quot;
&quot;os/exec&quot;
&quot;time&quot;
)
func main() {
f, err := os.Create(&quot;outputTest2_2.txt&quot;)
if err != nil {
panic(err)
}
defer f.Close()
currentTime, err := time.Now().MarshalText()
if err != nil {
panic(err)
}
f.Write(append(currentTime, &#39;\n&#39;))
msg, err := exec.Command(&quot;go&quot;, &quot;env&quot;).Output()
if err != nil {
panic(err)
}
f.Write(msg)
}

huangapple
  • 本文由 发表于 2021年7月17日 06:00:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/68415663.html
匿名

发表评论

匿名网友

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

确定