英文:
Go - Load data into three Struct using a common method
问题
我想使用一个方法(指针)将数据加载到三个结构体中,该方法解析一个文件。我对Go语言还不熟悉。
type a struct {
a string
}
type b struct {
b string
}
type c struct {
c string
}
func main() {
parse_file(filename)
}
// 在下面的方法中,*l不确定如何定义,以包含3个结构体
func (l *l) parse_file(filename string) {
// 逐行打开文件
// 如果行的值为a,则加载到结构体a中
// 如果行的值为b,则加载到结构体b中
// 如果行的值为c,则加载到结构体c中
}
以上是您提供的代码段的翻译。
英文:
I want to load data into three structs using a method(pointer) parsing a file. I am new to Go.
type a struct {
a string
}
type b struct {
b string
}
type c struct {
c string
}
func main() {
parse_file(filename)
}
//In below method, *l not sure how to define which can include 3 struct
func (l *l)parse_file(filename string) {
// open a file line by line
// if the line has value a then load into Struct a
//If the line has value b then load into Struct b
//If the line has value C then load into Struct c
}
答案1
得分: 1
你可以定义一个结构体,其中嵌入其他三个结构体。
看一下示例:
test.txt
a - This is A
b - This is B
c - This is C
代码
package main
import (
"bufio"
"fmt"
"os"
"strings"
)
type a struct {
a string
}
type b struct {
b string
}
type c struct {
c string
}
type l struct {
a
b
c
}
func main() {
l := &l{}
filename := "test.txt"
l.parse_file(filename)
fmt.Println("A : ", l.a)
fmt.Println("B : ", l.b)
fmt.Println("C : ", l.c)
}
// 在下面的方法中,*l 不确定如何定义,以包含3个结构体
func (l *l) parse_file(filename string) {
// 打开文件
file, err := os.Open(filename)
if err != nil {
panic(err)
}
defer file.Close()
// 逐行读取
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
switch {
// 如果行的值为 a,则加载到结构体 a 中
case strings.HasPrefix(line, "a -"):
// 将数据添加到 a 对象中
l.a = a{
a: line,
}
// 如果行的值为 b,则加载到结构体 b 中
case strings.HasPrefix(line, "b -"):
// 将数据添加到 b 对象中
l.b = b{
b: line,
}
// 如果行的值为 c,则加载到结构体 c 中
case strings.HasPrefix(line, "c -"):
// 将数据添加到 c 对象中
l.c = c{
c: line,
}
}
}
}
如果你想要移除前缀 a -
、b -
或 c -
,你可以使用 TrimPrefix
进行移除
line := strings.TrimPrefix(line, "a -")
英文:
You can define a struct which embed the other 3 struct
See the sample
test.txt
a - This is A
b - This is B
c - This is C
Code
package main
import (
"bufio"
"fmt"
"os"
"strings"
)
type a struct {
a string
}
type b struct {
b string
}
type c struct {
c string
}
type l struct {
a
b
c
}
func main() {
l := &l{}
filename := "test.txt"
l.parse_file(filename)
fmt.Println("A : ", l.a)
fmt.Println("B : ", l.b)
fmt.Println("C : ", l.c)
}
// In below method, *l not sure how to define which can include 3 struct
func (l *l) parse_file(filename string) {
// open a file
file, err := os.Open(filename)
if err != nil {
panic(err)
}
defer file.Close()
// read line by line
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
switch {
// if the line has value a then load into Struct a
case strings.HasPrefix(line, "a -"):
// add the data to the a object
l.a = a{
a: line,
}
//If the line has value b then load into Struct b
case strings.HasPrefix(line, "b -"):
// add the data to the b object
l.b = b{
b: line,
}
//If the line has value C then load into Struct c
case strings.HasPrefix(line, "c -"):
// add the data to the c object
l.c = c{
c: line,
}
}
}
}
If you want to remove the prefix a -
, b -
or c -
, then you can remove it with TrimPrefix
line := strings.TrimPrefix(line, "a -")
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论