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

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

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类型来逐行读取文件内容。下面是一个示例代码:

  1. package main
  2. import (
  3. "bufio"
  4. "fmt"
  5. "os"
  6. )
  7. func main() {
  8. // 打开文件
  9. file, err := os.Open("file.txt")
  10. if err != nil {
  11. fmt.Println("无法打开文件:", err)
  12. return
  13. }
  14. defer file.Close()
  15. // 创建一个Scanner来读取文件内容
  16. scanner := bufio.NewScanner(file)
  17. // 逐行读取文件内容
  18. for scanner.Scan() {
  19. line := scanner.Text()
  20. // 在这里可以对每一行的内容进行处理
  21. fmt.Println(line)
  22. }
  23. if err := scanner.Err(); err != nil {
  24. fmt.Println("读取文件时发生错误:", err)
  25. }
  26. }

你可以将上述代码保存为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预打开的文件描述符。

以下代码应该可以工作:

  1. package main
  2. import (
  3. "bufio"
  4. "fmt"
  5. "io"
  6. "log"
  7. "os"
  8. )
  9. func main() {
  10. const readBufLen = 1024
  11. reader := bufio.NewReader(os.Stdin)
  12. readBuf := make([]byte, readBufLen)
  13. content := make([]byte, 0, readBufLen)
  14. for {
  15. n, err := reader.Read(readBuf)
  16. if err != nil && err != io.EOF {
  17. log.Fatal(err)
  18. }
  19. if n != 0 {
  20. content = append(content, readBuf[:n]...)
  21. }
  22. if err == io.EOF {
  23. break
  24. }
  25. }
  26. fmt.Printf("%s\n", content)
  27. }
英文:

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:

  1. package main
  2. import (
  3. &quot;bufio&quot;
  4. &quot;fmt&quot;
  5. &quot;io&quot;
  6. &quot;log&quot;
  7. &quot;os&quot;
  8. )
  9. func main() {
  10. const readBufLen = 1024
  11. reader := bufio.NewReader(os.Stdin)
  12. readBuf := make([]byte, readBufLen)
  13. content := make([]byte, 0, readBufLen)
  14. for {
  15. n, err := reader.Read(readBuf)
  16. if err != nil &amp;&amp; err != io.EOF {
  17. log.Fatal(err)
  18. }
  19. if n != 0 {
  20. content = append(content, readBuf[:n]...)
  21. }
  22. if err == io.EOF {
  23. break
  24. }
  25. }
  26. fmt.Printf(&quot;%s\n&quot;, content)
  27. }

答案2

得分: 3

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

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

  1. package main
  2. import (
  3. "fmt"
  4. "io"
  5. "log"
  6. "os"
  7. )
  8. func main() {
  9. p, err := io.ReadAll(os.Stdin)
  10. if err != nil {
  11. log.Fatal(err)
  12. }
  13. // 根据应用程序的要求,移除前两个字节。
  14. if len(p) >= 2 {
  15. p = p[2:]
  16. }
  17. s := string(p)
  18. fmt.Println(s)
  19. }
英文:

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,

  1. package main
  2. import (
  3. &quot;fmt&quot;
  4. &quot;io&quot;
  5. &quot;log&quot;
  6. &quot;os&quot;
  7. )
  8. func main() {
  9. p, err := io.ReadAll(os.Stdin)
  10. if err != nil {
  11. log.Fatal(err)
  12. }
  13. // remove first two bytes per application requirements.
  14. if len(p) &gt;= 2 {
  15. p = p[2:]
  16. }
  17. s := string(p)
  18. fmt.Println(s)
  19. }

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:

确定