1
0
Fork 0
mirror of https://github.com/imjasonh/csvstruct synced 2026-07-07 02:32:20 +00:00
No description
Find a file
Jason Hall 0d469dc0aa
update to use Go module and GitHub Actions
Signed-off-by: Jason Hall <jason@chainguard.dev>
2023-09-30 19:50:13 -04:00
.github/workflows update to use Go module and GitHub Actions 2023-09-30 19:50:13 -04:00
bench_test.go Clean up tests, account for pre-1.4 csv encoding with a const bool 2014-12-22 17:57:10 -05:00
decode.go fix bug with decoding with tag names and omitempty 2014-12-22 17:00:56 -05:00
decode_test.go Clean up tests, account for pre-1.4 csv encoding with a const bool 2014-12-22 17:57:10 -05:00
encode.go gofmt -s 2015-03-23 11:11:25 -04:00
encode_test.go Clean up tests, account for pre-1.4 csv encoding with a const bool 2014-12-22 17:57:10 -05:00
go.mod update to use Go module and GitHub Actions 2023-09-30 19:50:13 -04:00
LICENSE Initial commit 2014-05-15 08:23:53 -07:00
README.md update to use Go module and GitHub Actions 2023-09-30 19:50:13 -04:00
roundtrip_test.go Support pointers in decoding 2014-12-22 16:50:22 -05:00

GoDoc GitHub Actions Build Status

This library provides methods to read and write CSV data into and from Go structs.

Decoding

Given the following CSV data:

Name,Age,Height
Alice,25,5.7
Bob,24,5.9

You could decode the data into structs like so:

f, _ := os.Open("path/to/your.csv")
defer f.Close()
type Person struct {
	Name string
	Age int
	Height float64
}
var p Person
d := csvstruct.NewDecoder(f)
for {
	if err := d.DecodeNext(&p); err == io.EOF {
		break
	} else if err != nil {
		// handle error
	}
	fmt.Printf('%s's age is %d\n", p.Name, p.Age)
}

Encoding

Similarly, given structs, you can generate CSV data.

people := []Person{{"Carl", 31, 6.0}, {"Debbie", 27, 5.3}}
e := csvstruct.NewEncoder(os.Stdout)
for _, p := range people {
	if err := e.EncodeNext(p); err != nil {
		// handle error
	}
}

Struct tags are supported to override the struct's field names and ignore fields. See the GoDoc for more information and tests for more examples.


License

Copyright 2014 Jason Hall

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.