go-hep.org/x/hep@v0.38.1/hbook/ntup/ntcsv/example_test.go (about) 1 // Copyright ©2017 The go-hep Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package ntcsv_test 6 7 import ( 8 "fmt" 9 "log" 10 11 "go-hep.org/x/hep/hbook/ntup/ntcsv" 12 ) 13 14 func ExampleOpen() { 15 // Open a new n-tuple pointing at a CSV file "testdata/simple.csv" 16 // whose field separator is ';'. 17 // We rename the columns v1, v2 and v3. 18 nt, err := ntcsv.Open( 19 "testdata/simple.csv", 20 ntcsv.Comma(';'), 21 ntcsv.Columns("v1", "v2", "v3"), 22 ) 23 if err != nil { 24 log.Fatal(err) 25 } 26 defer func() { 27 err = nt.DB().Close() 28 if err != nil { 29 log.Fatal(err) 30 } 31 }() 32 33 err = nt.Scan("v1, v2, v3", func(i int64, f float64, s string) error { 34 fmt.Printf("%d %f %q\n", i, f, s) 35 return nil 36 }) 37 if err != nil { 38 log.Fatal(err) 39 } 40 41 // Output: 42 // 0 0.000000 "str-0" 43 // 1 1.000000 "str-1" 44 // 2 2.000000 "str-2" 45 // 3 3.000000 "str-3" 46 // 4 4.000000 "str-4" 47 // 5 5.000000 "str-5" 48 // 6 6.000000 "str-6" 49 // 7 7.000000 "str-7" 50 // 8 8.000000 "str-8" 51 // 9 9.000000 "str-9" 52 } 53 54 func ExampleOpen_fromRemote() { 55 // Open a new n-tuple pointing at a remote CSV file 56 // "https://codeberg.org/go-hep/hep/raw/branch/main/hbook/ntup/ntcsv/testdata/simple.csv" 57 // whose field separator is ';'. 58 // We rename the columns v1, v2 and v3. 59 nt, err := ntcsv.Open( 60 "https://codeberg.org/go-hep/hep/raw/branch/main/hbook/ntup/ntcsv/testdata/simple.csv", 61 ntcsv.Comma(';'), 62 ntcsv.Columns("v1", "v2", "v3"), 63 ) 64 if err != nil { 65 log.Fatal(err) 66 } 67 defer func() { 68 err = nt.DB().Close() 69 if err != nil { 70 log.Fatal(err) 71 } 72 }() 73 74 err = nt.Scan("v1, v2, v3", func(i int64, f float64, s string) error { 75 fmt.Printf("%d %f %q\n", i, f, s) 76 return nil 77 }) 78 if err != nil { 79 log.Fatal(err) 80 } 81 82 // Output: 83 // 0 0.000000 "str-0" 84 // 1 1.000000 "str-1" 85 // 2 2.000000 "str-2" 86 // 3 3.000000 "str-3" 87 // 4 4.000000 "str-4" 88 // 5 5.000000 "str-5" 89 // 6 6.000000 "str-6" 90 // 7 7.000000 "str-7" 91 // 8 8.000000 "str-8" 92 // 9 9.000000 "str-9" 93 } 94 95 func ExampleOpen_withDefaultVarNames() { 96 // Open a new n-tuple pointing at a CSV file "testdata/simple.csv" 97 // whose field separator is ';'. 98 // We use the default column names: var1, var2, var3, ... 99 nt, err := ntcsv.Open( 100 "testdata/simple.csv", 101 ntcsv.Comma(';'), 102 ) 103 if err != nil { 104 log.Fatal(err) 105 } 106 defer func() { 107 err = nt.DB().Close() 108 if err != nil { 109 log.Fatal(err) 110 } 111 }() 112 113 err = nt.Scan("var1, var2, var3", func(i int64, f float64, s string) error { 114 fmt.Printf("%d %f %q\n", i, f, s) 115 return nil 116 }) 117 if err != nil { 118 log.Fatal(err) 119 } 120 121 // Output: 122 // 0 0.000000 "str-0" 123 // 1 1.000000 "str-1" 124 // 2 2.000000 "str-2" 125 // 3 3.000000 "str-3" 126 // 4 4.000000 "str-4" 127 // 5 5.000000 "str-5" 128 // 6 6.000000 "str-6" 129 // 7 7.000000 "str-7" 130 // 8 8.000000 "str-8" 131 // 9 9.000000 "str-9" 132 } 133 134 func ExampleOpen_withHeader() { 135 // Open a new n-tuple pointing at a CSV file "testdata/simple.csv" 136 // whose field separator is ';'. 137 // We rename the columns v1, v2 and v3. 138 // We tell the CSV driver to handle the CSV header. 139 nt, err := ntcsv.Open( 140 "testdata/simple-with-header.csv", 141 ntcsv.Comma(';'), 142 ntcsv.Header(), 143 ntcsv.Columns("v1", "v2", "v3"), 144 ) 145 if err != nil { 146 log.Fatal(err) 147 } 148 defer func() { 149 err = nt.DB().Close() 150 if err != nil { 151 log.Fatal(err) 152 } 153 }() 154 155 err = nt.Scan("v1, v2, v3", func(i int64, f float64, s string) error { 156 fmt.Printf("%d %f %q\n", i, f, s) 157 return nil 158 }) 159 if err != nil { 160 log.Fatal(err) 161 } 162 163 // Output: 164 // 0 0.000000 "str-0" 165 // 1 1.000000 "str-1" 166 // 2 2.000000 "str-2" 167 // 3 3.000000 "str-3" 168 // 4 4.000000 "str-4" 169 // 5 5.000000 "str-5" 170 // 6 6.000000 "str-6" 171 // 7 7.000000 "str-7" 172 // 8 8.000000 "str-8" 173 // 9 9.000000 "str-9" 174 } 175 176 func ExampleOpen_withHeaderAndImplicitColumns() { 177 // Open a new n-tuple pointing at a CSV file "testdata/simple.csv" 178 // whose field separator is ';'. 179 // We tell the CSV driver to handle the CSV header. 180 // And we implicitly use the column names for the queries. 181 nt, err := ntcsv.Open( 182 "testdata/simple-with-header.csv", 183 ntcsv.Comma(';'), 184 ntcsv.Header(), 185 ) 186 if err != nil { 187 log.Fatal(err) 188 } 189 defer func() { 190 err = nt.DB().Close() 191 if err != nil { 192 log.Fatal(err) 193 } 194 }() 195 196 err = nt.Scan("i, f, str", func(i int64, f float64, s string) error { 197 fmt.Printf("%d %f %q\n", i, f, s) 198 return nil 199 }) 200 if err != nil { 201 log.Fatal(err) 202 } 203 204 // Output: 205 // 0 0.000000 "str-0" 206 // 1 1.000000 "str-1" 207 // 2 2.000000 "str-2" 208 // 3 3.000000 "str-3" 209 // 4 4.000000 "str-4" 210 // 5 5.000000 "str-5" 211 // 6 6.000000 "str-6" 212 // 7 7.000000 "str-7" 213 // 8 8.000000 "str-8" 214 // 9 9.000000 "str-9" 215 } 216 217 func ExampleOpen_withHeaderAndExlicitColumns() { 218 // Open a new n-tuple pointing at a CSV file "testdata/simple.csv" 219 // whose field separator is ';'. 220 // We tell the CSV driver to handle the CSV header. 221 // And we explicitly use our column names for the queries. 222 nt, err := ntcsv.Open( 223 "testdata/simple-with-header.csv", 224 ntcsv.Comma(';'), 225 ntcsv.Header(), 226 ntcsv.Columns("v1", "v2", "v3"), 227 ) 228 if err != nil { 229 log.Fatal(err) 230 } 231 defer func() { 232 err = nt.DB().Close() 233 if err != nil { 234 log.Fatal(err) 235 } 236 }() 237 238 err = nt.Scan("v1, v2, v3", func(i int64, f float64, s string) error { 239 fmt.Printf("%d %f %q\n", i, f, s) 240 return nil 241 }) 242 if err != nil { 243 log.Fatal(err) 244 } 245 246 // Output: 247 // 0 0.000000 "str-0" 248 // 1 1.000000 "str-1" 249 // 2 2.000000 "str-2" 250 // 3 3.000000 "str-3" 251 // 4 4.000000 "str-4" 252 // 5 5.000000 "str-5" 253 // 6 6.000000 "str-6" 254 // 7 7.000000 "str-7" 255 // 8 8.000000 "str-8" 256 // 9 9.000000 "str-9" 257 }