ssa
94 строки · 2.4 Кб
1package gomathtests2
3import (4"bufio"5"fmt"6"os"7"strconv"8"strings"9)
10
11// Загрузить данные из файла
12func LoadSlice(FilePathName string) (data []float64, Err error) {13// Если грузим в txt14if strings.Contains(FilePathName, ".txt") {15file, ErrOpen := os.Open(FilePathName)16if ErrOpen != nil {17return nil, fmt.Errorf("os.Open: %v", ErrOpen)18}19defer file.Close()20scanner := bufio.NewScanner(file)21for scanner.Scan() {22fl, ErrParseFloat := strconv.ParseFloat(scanner.Text(), 64)23if ErrParseFloat != nil {24return nil, fmt.Errorf("strconv.ParseFloat: %v", ErrParseFloat)25}26data = append(data, fl)27}28if Err = scanner.Err(); Err != nil {29return nil, fmt.Errorf("scanner.Err(): %v", Err)30}31}32
33// // Если грузим в xlsx34// if strings.Contains(FilePathName, ".xlsx") {35
36// }37return data, Err38}
39
40// Загрузить RGB сигонал
41func LoadRGB(FileName string) (R, G, B []float64, Err error) {42file, ErrOpen := os.Open(FileName)43if ErrOpen != nil {44return nil, nil, nil, fmt.Errorf("os.Open: %v", ErrOpen)45}46defer file.Close()47scanner := bufio.NewScanner(file)48// optionally, resize scanner's capacity for lines over 64K, see next example49for scanner.Scan() {50str := scanner.Text()51strs := strings.Split(str, ";")52if len(strs) == 3 {53strs[0] = strings.ReplaceAll(strs[0], ",", ".")54strs[1] = strings.ReplaceAll(strs[1], ",", ".")55strs[2] = strings.ReplaceAll(strs[2], ",", ".")56
57r, _ := strconv.ParseFloat(strs[0], 64)58g, _ := strconv.ParseFloat(strs[1], 64)59b, _ := strconv.ParseFloat(strs[2], 64)60
61R = append(R, r)62G = append(G, g)63B = append(B, b)64}65}66if ErrScanner := scanner.Err(); ErrScanner != nil {67return nil, nil, nil, fmt.Errorf("scanner.Err: %v", ErrScanner)68}69return R, G, B, nil70}
71
72func Save(FileName string, opts ...[]float64) error {73filePW, ErrOpenFile := os.Create(FileName)74if ErrOpenFile != nil {75panic(ErrOpenFile)76}77defer filePW.Close()78
79// var content string80for _, slice := range opts {81// fmt.Println(slice)82for i := range slice {83strs := make([]string, len(opts))84for j := range opts {85strs[j] = fmt.Sprintf("%.8f", opts[j][i])86}87// fmt.Println()88if _, ErrWriteString := filePW.WriteString(strings.Join(strs, ";") + "\n"); ErrWriteString != nil {89return fmt.Errorf("filePW.WriteString: %v", ErrWriteString)90}91}92}93return nil94}
95