Go – Load data into three Struct using a common method

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

Go - Load data into three Struct using a common method

问题

我想使用一个方法(指针)将数据加载到三个结构体中,该方法解析一个文件。我对Go语言还不熟悉。

  1. type a struct {
  2. a string
  3. }
  4. type b struct {
  5. b string
  6. }
  7. type c struct {
  8. c string
  9. }
  10. func main() {
  11. parse_file(filename)
  12. }
  13. // 在下面的方法中,*l不确定如何定义,以包含3个结构体
  14. func (l *l) parse_file(filename string) {
  15. // 逐行打开文件
  16. // 如果行的值为a,则加载到结构体a中
  17. // 如果行的值为b,则加载到结构体b中
  18. // 如果行的值为c,则加载到结构体c中
  19. }

以上是您提供的代码段的翻译。

英文:

I want to load data into three structs using a method(pointer) parsing a file. I am new to Go.

  1. type a struct {
  2. a string
  3. }
  4. type b struct {
  5. b string
  6. }
  7. type c struct {
  8. c string
  9. }
  10. func main() {
  11. parse_file(filename)
  12. }
  13. //In below method, *l not sure how to define which can include 3 struct
  14. func (l *l)parse_file(filename string) {
  15. // open a file line by line
  16. // if the line has value a then load into Struct a
  17. //If the line has value b then load into Struct b
  18. //If the line has value C then load into Struct c
  19. }

答案1

得分: 1

你可以定义一个结构体,其中嵌入其他三个结构体。

看一下示例:

test.txt

  1. a - This is A
  2. b - This is B
  3. c - This is C

代码

  1. package main
  2. import (
  3. "bufio"
  4. "fmt"
  5. "os"
  6. "strings"
  7. )
  8. type a struct {
  9. a string
  10. }
  11. type b struct {
  12. b string
  13. }
  14. type c struct {
  15. c string
  16. }
  17. type l struct {
  18. a
  19. b
  20. c
  21. }
  22. func main() {
  23. l := &l{}
  24. filename := "test.txt"
  25. l.parse_file(filename)
  26. fmt.Println("A : ", l.a)
  27. fmt.Println("B : ", l.b)
  28. fmt.Println("C : ", l.c)
  29. }
  30. // 在下面的方法中,*l 不确定如何定义,以包含3个结构体
  31. func (l *l) parse_file(filename string) {
  32. // 打开文件
  33. file, err := os.Open(filename)
  34. if err != nil {
  35. panic(err)
  36. }
  37. defer file.Close()
  38. // 逐行读取
  39. scanner := bufio.NewScanner(file)
  40. for scanner.Scan() {
  41. line := scanner.Text()
  42. switch {
  43. // 如果行的值为 a,则加载到结构体 a 中
  44. case strings.HasPrefix(line, "a -"):
  45. // 将数据添加到 a 对象中
  46. l.a = a{
  47. a: line,
  48. }
  49. // 如果行的值为 b,则加载到结构体 b 中
  50. case strings.HasPrefix(line, "b -"):
  51. // 将数据添加到 b 对象中
  52. l.b = b{
  53. b: line,
  54. }
  55. // 如果行的值为 c,则加载到结构体 c 中
  56. case strings.HasPrefix(line, "c -"):
  57. // 将数据添加到 c 对象中
  58. l.c = c{
  59. c: line,
  60. }
  61. }
  62. }
  63. }

如果你想要移除前缀 a -b -c -,你可以使用 TrimPrefix 进行移除

  1. line := strings.TrimPrefix(line, "a -")
英文:

You can define a struct which embed the other 3 struct

See the sample

test.txt

  1. a - This is A
  2. b - This is B
  3. c - This is C

Code

  1. package main
  2. import (
  3. "bufio"
  4. "fmt"
  5. "os"
  6. "strings"
  7. )
  8. type a struct {
  9. a string
  10. }
  11. type b struct {
  12. b string
  13. }
  14. type c struct {
  15. c string
  16. }
  17. type l struct {
  18. a
  19. b
  20. c
  21. }
  22. func main() {
  23. l := &l{}
  24. filename := "test.txt"
  25. l.parse_file(filename)
  26. fmt.Println("A : ", l.a)
  27. fmt.Println("B : ", l.b)
  28. fmt.Println("C : ", l.c)
  29. }
  30. // In below method, *l not sure how to define which can include 3 struct
  31. func (l *l) parse_file(filename string) {
  32. // open a file
  33. file, err := os.Open(filename)
  34. if err != nil {
  35. panic(err)
  36. }
  37. defer file.Close()
  38. // read line by line
  39. scanner := bufio.NewScanner(file)
  40. for scanner.Scan() {
  41. line := scanner.Text()
  42. switch {
  43. // if the line has value a then load into Struct a
  44. case strings.HasPrefix(line, "a -"):
  45. // add the data to the a object
  46. l.a = a{
  47. a: line,
  48. }
  49. //If the line has value b then load into Struct b
  50. case strings.HasPrefix(line, "b -"):
  51. // add the data to the b object
  52. l.b = b{
  53. b: line,
  54. }
  55. //If the line has value C then load into Struct c
  56. case strings.HasPrefix(line, "c -"):
  57. // add the data to the c object
  58. l.c = c{
  59. c: line,
  60. }
  61. }
  62. }
  63. }

If you want to remove the prefix a - , b - or c -, then you can remove it with TrimPrefix

  1. line := strings.TrimPrefix(line, "a -")

huangapple
  • 本文由 发表于 2023年7月4日 14:37:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/76609926.html
匿名

发表评论

匿名网友

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

确定