如何使用golang过滤给定数据中的元素?

This is the 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 I filter the college name (for example: --flag=SR3) the output should print under SR3 , what are the names and depts are there.

A4 EEE

A10 MECH
  1. You need to design the data model for your data. You have several alternatives here but most simple one is struct with three fields: for name, for department and for college. The data is in the array. Read about data structures here: https://golang.org/doc/effective_go.html#data
  2. You need to initialise your data structure. Maybe you have the input in text file? Then you can use methods of the fmt or bufio to read it.
  3. They you need to read the command line arguments - get them from os.Args.
  4. The filter data and output the result. This is a simple for loop over the slice with one if in it. Use Println to output the 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"})
  ...

  // get filter from os.Args
  filter := ...

  for _, v := range records {
    if v.College == filter {
      Println(v.Name, v.Department)
    }
  }
}

P.S. If you ask yourself why the question is downvoted (not by me) - because it does not demonstrate the you actually tried to solve the problem. See https://stackoverflow.com/help/on-topic