go - How to filter the elements in the given data using golang? -
this input data:
name dept college a1 cse sr1 a2 cse sr2 a3 ece sr1 a4 eee sr3 a5 ece sr1 a6 mech sr2 a7 cse sr1 a8 eee sr1 a9 ece sr3 a10 mech sr3 a11 eee sr1
expected output: if filter college name (for example: --flag=sr3
) output should print under sr3
, names , depts there.
a4 eee a10 mech
- you need design data model data. have several alternatives here simple 1 struct 3 fields: name, department , college. data in array. read data structures here: https://golang.org/doc/effective_go.html#data
- you need initialise data structure. maybe have input in text file? can use methods of fmt or bufio read it.
- they need read command line arguments - them os.args.
- the filter data , output result. simple
for
loop on slice 1if
in it. use println output result.
sample code:
package main import ( . "fmt" "os" ) type record struct { name string department string college string } func main() { records := make([]record, 0) // add records records = append(records, record{"a1", "cse", "sr1"}) ... // filter os.args filter := ... _, v := range records { if v.college == filter { println(v.name, v.department) } } }
p.s. if ask why question downvoted (not me) - because not demonstrate tried solve problem. see https://stackoverflow.com/help/on-topic
Comments
Post a Comment