how to read a file as input and use its content in golang?

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

how to read a file as input and use its content in golang?

问题

你好!以下是关于如何读取文本文件(.txt)并使用其内容的解释:

要读取文本文件并将其内容作为输入,你可以使用编程语言中的文件读取功能。根据你提到的命令go run script.go < file.txt,我猜测你正在使用Go语言。

在Go语言中,你可以使用os包中的Open函数打开文件,然后使用bufio包中的Scanner类型来逐行读取文件内容。下面是一个示例代码:

package main

import (
	"bufio"
	"fmt"
	"os"
)

func main() {
	// 打开文件
	file, err := os.Open("file.txt")
	if err != nil {
		fmt.Println("无法打开文件:", err)
		return
	}
	defer file.Close()

	// 创建一个Scanner来读取文件内容
	scanner := bufio.NewScanner(file)

	// 逐行读取文件内容
	for scanner.Scan() {
		line := scanner.Text()
		// 在这里可以对每一行的内容进行处理
		fmt.Println(line)
	}

	if err := scanner.Err(); err != nil {
		fmt.Println("读取文件时发生错误:", err)
	}
}

你可以将上述代码保存为script.go文件,然后在终端中运行go run script.go < file.txt命令来读取file.txt文件的内容。

希望这能帮到你!如果还有其他问题,请随时提问。

英文:

can someone explain me how to read a text file (.txt) as the input and use its content?<br />
have to run this command:<br/>
go run script.go &lt; file.txt<br />
can't find nothing that could help me, even after many researches on the internet!<br />
thanks in advance<br/>
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀

答案1

得分: 3

你可以从标准输入(stdin)中读取内容。当你使用'<'符号时,简单地表示STDIN
在Go语言中,要从标准输入中读取内容,你需要使用os.Stdin预打开的文件描述符。

以下代码应该可以工作:

package main

import (
	"bufio"
	"fmt"
	"io"
	"log"
	"os"
)

func main() {
	const readBufLen = 1024
	reader := bufio.NewReader(os.Stdin)
	readBuf := make([]byte, readBufLen)
	content := make([]byte, 0, readBufLen)
	for {
		n, err := reader.Read(readBuf)
		if err != nil && err != io.EOF {
			log.Fatal(err)
		}
		if n != 0 {
			content = append(content, readBuf[:n]...)
		}
		if err == io.EOF {
			break
		}
	}

	fmt.Printf("%s\n", content)
}
英文:

You can read from stdin. When you use '<' it is simple means STDIN.
To read from STDIN in Go you need to use os.Stdin pre-opened file descriptor.

This code should work:

package main

import (
	&quot;bufio&quot;
	&quot;fmt&quot;
	&quot;io&quot;
	&quot;log&quot;
	&quot;os&quot;
)

func main() {
	const readBufLen = 1024
	reader := bufio.NewReader(os.Stdin)
	readBuf := make([]byte, readBufLen)
	content := make([]byte, 0, readBufLen)
	for {
		n, err := reader.Read(readBuf)
		if err != nil &amp;&amp; err != io.EOF {
			log.Fatal(err)
		}
		if n != 0 {
			content = append(content, readBuf[:n]...)
		}
		if err == io.EOF {
			break
		}
	}

	fmt.Printf(&quot;%s\n&quot;, content)
}

答案2

得分: 3

使用io.ReadAll函数将os.Stdin的内容读取到一个[]byte中。

根据应用程序的要求,使用slice表达式忽略stdin的前两个字节。

package main

import (
	"fmt"
	"io"
	"log"
	"os"
)

func main() {
	p, err := io.ReadAll(os.Stdin)
	if err != nil {
		log.Fatal(err)
	}

	// 根据应用程序的要求,移除前两个字节。
	if len(p) >= 2 {
		p = p[2:]
	}

	s := string(p)
	fmt.Println(s)
}
英文:

Use io.ReadAll to slurp the contents of os.Stdin to a []byte.

Use a slice expression ignore the first two bytes of stdin per application requirements,

package main

import (
	&quot;fmt&quot;
	&quot;io&quot;
	&quot;log&quot;
	&quot;os&quot;
)

func main() {
	p, err := io.ReadAll(os.Stdin)
	if err != nil {
		log.Fatal(err)
	}

	// remove first two bytes per application requirements.
	if len(p) &gt;= 2 {
		p = p[2:]
	}

	s := string(p)
	fmt.Println(s)
}

huangapple
  • 本文由 发表于 2023年6月18日 19:04:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/76500214.html
匿名

发表评论

匿名网友

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

确定