{"id":21314114,"url":"https://github.com/bitxx/csvreader","last_synced_at":"2025-03-15T21:14:25.946Z","repository":{"id":57653493,"uuid":"452694373","full_name":"bitxx/csvreader","owner":"bitxx","description":"csv解析为结构体，对原作者https://github.com/zhnxin/csvreader项目的进一步完善","archived":false,"fork":false,"pushed_at":"2023-07-26T13:25:33.000Z","size":5,"stargazers_count":0,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-01-22T10:16:06.438Z","etag":null,"topics":["csv","decode","encode","golang","parse"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/bitxx.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2022-01-27T13:31:17.000Z","updated_at":"2023-07-31T17:20:52.000Z","dependencies_parsed_at":"2023-07-26T14:06:15.800Z","dependency_job_id":null,"html_url":"https://github.com/bitxx/csvreader","commit_stats":null,"previous_names":["bitxx/csvreader","jason-wj/csvreader"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bitxx%2Fcsvreader","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bitxx%2Fcsvreader/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bitxx%2Fcsvreader/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bitxx%2Fcsvreader/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bitxx","download_url":"https://codeload.github.com/bitxx/csvreader/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243790999,"owners_count":20348385,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["csv","decode","encode","golang","parse"],"created_at":"2024-11-21T18:10:40.535Z","updated_at":"2025-03-15T21:14:25.924Z","avatar_url":"https://github.com/bitxx.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# csvreader\nJason \u003cidea_wj@163.com\u003e\n简单的csv格式文件解析到`struct`工具\n\n2022-01-27 注：为满足自身需要，在 [原作者：zhnxin 项目](https://github.com/zhnxin/csvreader) 基础上做了改动：\n1. 当header中字段首尾存在空格时，去除空格\n2. value值，去除开头和结尾的空格\n3. header中的字段必须全为小写，结构体随意，但名称必须和header中字段名称保持一致\n\n## 安装\n```shell\ngo get github.com/bitxx/csvreader\n```\n\n## 简单用法\n\nNOTE: 默认情况下，*csv* 文件的首行会被当作header处理。\n\n```csv\nhosname,ip\nredis,172.17.0.2\nmariadb,172.17.0.3\n```\n\n```go\ntype Info struct{\n    Hostname string\n    IP string\n}\n\n//struct slice\ninfos := []Info{}\n_ = csvreader.New().UnMarshalFile(\"file.csv\",\u0026infos)\nbody,_ := json.Marshal(infos)\nfmt.Println(string(body))\n\n//point slice\ninfos = []*Info{}\n_ = csvreader.New().UnMarshalFile(\"file.csv\",\u0026infos)\nbody,_ := json.Marshal(infos)\nfmt.Println(string(body))\n```\n\nNOTE: 如果 *csv* 文件首行不包含header，可以使用 *WithHeader([]string)* 来指定header。\n\n```go\n_ = csvreader.New().WithHeader([]string{\"hostname\",\"ip\"}).UnMarshalFile(\"file.csv\",\u0026infos)\n```\n\n## 自定义parster\n\n就像枚举类型(enum),偶尔会遇到这种需要实现自定义转换过程的情况。例子如下\n\n```go\ntype NetProtocol uint32\nconst(\n    NetProtocol_TCP NetProtocol = iota\n    NetProtocol_UDP\n    NetProtocol_DCCP\n    NetProtocol_SCTP\n)\n\ntype ServiceInfo struct{\n    Host string\n    Port string\n    Protocol NetProtocol\n}\n```\n\n直接使用原始的类型来编辑csv文件，十分不便。这时就需要实现自定义parser。\n\n```go\ntype CsvMarshal interface {\n    FromString(string) error\n}\n```\n\n```go\nfunc (p *NetProtocol)FromString(protocol string) error{\nswitch strings.ToLower(protocol){\n    case \"tcp\":\n        *p = NetProtocol_TCP\n    case \"udp\":\n        *p = NetProtocol_UDP\n    case \"dccp\":\n        *p = NetProtocol_DCCP\n    case \"sctp\":\n        *p = NetProtocol_SCTP\n    default:\n        return fmt.Errorf(\"unknown protocoal:%s\",protocol)\n    }\n    return nil\n}\n```\n另外一个例子 reader_test.go TestCustom","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbitxx%2Fcsvreader","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbitxx%2Fcsvreader","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbitxx%2Fcsvreader/lists"}