// ReadProducts - reading products array from csv-file
reading products array from csv-file
// ReadProducts - reading products array from csv - file
func ReadProducts(filePath string) ([]domain.Product, error) {
	csvFile, err := os.Open(filePath)
	if err != nil {
		return nil, fmt.Errorf("open file fail: %w/n", err)
	}
	reader := csv.NewReader(csvFile)
	reader.Comma = READER_COMMA
	fileExt := filepath.Ext(filePath)
	if len(fileExt) > 1 {
		fileExt = fileExt[1:]
	}
	userHeader, _ := csvutil.Header(domain.Product{}, fileExt)
	dec, err := csvutil.NewDecoder(reader, userHeader...)
	if err != nil {
		return nil, fmt.Errorf("file header decode fail: %w/n", err)
	}
	var products []domain.Product
	for {
		var p domain.Product
		if err := dec.Decode(&p); err == io.EOF {
			break
		}
		if err != nil {
			return nil, fmt.Errorf("product decode fail: %w/n", err)
		}
		products = append(products, p)
	}
	return products, nil
}
func ReadProducts ( filePath string ) ( [ ] domain . Product , error ) { csvFile , err := os . Open ( filePath ) if err != nil { return nil , fmt . Errorf ( " " , err ) } reader := csv . NewReader ( csvFile ) reader . Comma = READER_COMMA fileExt := filepath . Ext ( filePath ) if len ( fileExt ) > 1 { fileExt = fileExt [ 1 : ] } userHeader , _ := csvutil . Header ( domain . Product { } , fileExt ) dec , err := csvutil . NewDecoder ( reader , userHeader ... ) if err != nil { return nil , fmt . Errorf ( " " , err ) } var products [ ] domain . Product for { var p domain . Product if err := dec . Decode ( & p ) ; err == io . EOF { break } if err != nil { return nil , fmt . Errorf ( " " , err ) } products = append ( products , p ) } return products , nil }
reading products array from csv-file
reading products array from csv - file