在Go语言中,for循环之外的未使用变量。

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

Unused variable outside of a for-loop in Go

问题

我无法编译以下Go代码。我一直收到一个错误,说变量'header'未被使用。我正在尝试读取和处理一个CSV文件。文件逐行读取,所以我需要将标题保存到一个“循环外”的变量中,以便在处理CSV行时可以引用它。有人知道我作为一个新的Go用户做错了什么吗?

  1. func main() {
  2. ...
  3. inputData := csv.NewReader(file)
  4. var header []string
  5. //keep reading the file until EOF is reached
  6. for j := 0; ; j++ {
  7. i, err := inputData.Read()
  8. if err == io.EOF {
  9. break
  10. }
  11. if j == 0 {
  12. header = i
  13. } else {
  14. // Business logic in here
  15. fmt.Println(i)
  16. //TODO convert to JSON
  17. //TODO send to SQS
  18. }
  19. }
  20. }

以下是翻译好的代码:

  1. func main() {
  2. ...
  3. inputData := csv.NewReader(file)
  4. var header []string
  5. // 读取文件直到达到EOF
  6. for j := 0; ; j++ {
  7. i, err := inputData.Read()
  8. if err == io.EOF {
  9. break
  10. }
  11. if j == 0 {
  12. header = i
  13. } else {
  14. // 在这里处理业务逻辑
  15. fmt.Println(i)
  16. //TODO 转换为JSON
  17. //TODO 发送到SQS
  18. }
  19. }
  20. }
英文:

I can't compile the following Go code. I keep getting an error that variable 'header' is not used. I'm trying to read and process a CSV file. The file is read line by line an so I need to save the header into a "outside-the-loop" variable I can refer to when processing CSV lines. Anyone knows what I'm doing incorrectly as a new Go user?

  1. func main() {
  2. ...
  3. inputData := csv.NewReader(file)
  4. var header []string
  5. //keep reading the file until EOF is reached
  6. for j := 0; ; j++ {
  7. i, err := inputData.Read()
  8. if err == io.EOF {
  9. break
  10. }
  11. if j == 0 {
  12. header = i
  13. } else {
  14. // Business logic in here
  15. fmt.Println(i)
  16. //TODO convert to JSON
  17. //TODO send to SQS
  18. }
  19. }
  20. }

答案1

得分: 2

你可以将一个空标识符赋值给变量,只是为了能够编译你的代码,例如:_ = header。以下是一个示例:

  1. package main
  2. import (
  3. "encoding/csv"
  4. "fmt"
  5. "io"
  6. "strings"
  7. )
  8. func main() {
  9. in := `first_name,last_name,username
  10. "Rob","Pike",rob
  11. Ken,Thompson,ken
  12. "Robert","Griesemer","gri"
  13. `
  14. file := strings.NewReader(in)
  15. inputData := csv.NewReader(file)
  16. var header []string
  17. //keep reading the file until EOF is reached
  18. for j := 0; ; j++ {
  19. i, err := inputData.Read()
  20. if err == io.EOF {
  21. break
  22. }
  23. if j == 0 {
  24. header = i
  25. } else {
  26. // Business logic in here
  27. fmt.Println(i)
  28. //TODO convert to JSON
  29. //TODO send to SQS
  30. }
  31. }
  32. _ = header
  33. }

你可以在这里运行这段代码:https://go.dev/play/p/BrmYU2zAc9f

英文:

You can assign to a blank identifier just to be able to compile your code, like: _ = header. As example:

  1. package main
  2. import (
  3. "encoding/csv"
  4. "fmt"
  5. "io"
  6. "strings"
  7. )
  8. func main() {
  9. in := `first_name,last_name,username
  10. "Rob","Pike",rob
  11. Ken,Thompson,ken
  12. "Robert","Griesemer","gri"
  13. `
  14. file := strings.NewReader(in)
  15. inputData := csv.NewReader(file)
  16. var header []string
  17. //keep reading the file until EOF is reached
  18. for j := 0; ; j++ {
  19. i, err := inputData.Read()
  20. if err == io.EOF {
  21. break
  22. }
  23. if j == 0 {
  24. header = i
  25. } else {
  26. // Business logic in here
  27. fmt.Println(i)
  28. //TODO convert to JSON
  29. //TODO send to SQS
  30. }
  31. }
  32. _ = header
  33. }

https://go.dev/play/p/BrmYU2zAc9f

huangapple
  • 本文由 发表于 2022年2月23日 04:00:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/71227596.html
匿名

发表评论

匿名网友

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

确定