dongzhong8834 2015-07-08 08:43
浏览 398

如何使用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条回答 默认 最新

  • duandi4238 2015-07-08 09:04
    关注
    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

    评论

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP