From c3af2face4ac6c922c23ebbbbc96db8940cefe9c Mon Sep 17 00:00:00 2001 From: "Arkadiy.Ohotnikov" Date: Wed, 30 Apr 2025 13:28:27 +0300 Subject: [PATCH 1/2] hw02_my_fix --- hw02_fix_app/go.mod | 2 -- hw02_fix_app/go.sum | 2 -- hw02_fix_app/main.go | 8 ++++---- hw02_fix_app/printer/pkg.go | 4 +++- hw02_fix_app/reader/pkg.go | 15 ++++++++------- hw02_fix_app/types/employee.go | 2 +- 6 files changed, 16 insertions(+), 17 deletions(-) diff --git a/hw02_fix_app/go.mod b/hw02_fix_app/go.mod index cd922a2..949c832 100644 --- a/hw02_fix_app/go.mod +++ b/hw02_fix_app/go.mod @@ -1,5 +1,3 @@ module github.com/fixme_my_friend/hw02_fix_app go 1.20 - -require golang.org/x/example v0.0.0-20230714141244-83a29069fa80 // indirect diff --git a/hw02_fix_app/go.sum b/hw02_fix_app/go.sum index 46ddcb7..e69de29 100644 --- a/hw02_fix_app/go.sum +++ b/hw02_fix_app/go.sum @@ -1,2 +0,0 @@ -golang.org/x/example v0.0.0-20230714141244-83a29069fa80 h1:dIO+42L/3wms+HG8CICCnwjAoQ9K7FD3JodZPpG5ymY= -golang.org/x/example v0.0.0-20230714141244-83a29069fa80/go.mod h1:DJ5Pz7jIW3ezl8PmYmOf5B1CZ98NJv/GaEcafLB3Lzk= diff --git a/hw02_fix_app/main.go b/hw02_fix_app/main.go index 45593d4..68db8a0 100644 --- a/hw02_fix_app/main.go +++ b/hw02_fix_app/main.go @@ -1,13 +1,14 @@ -package init +package main import ( + "fmt" + "github.com/fixme_my_friend/hw02_fix_app/printer" "github.com/fixme_my_friend/hw02_fix_app/reader" "github.com/fixme_my_friend/hw02_fix_app/types" - "fmt" ) -func init() { +func main() { var path string = "data.json" fmt.Printf("Enter data file path: ") @@ -18,7 +19,6 @@ func init() { if len(path) == 0 { path = "data.json" - } else { } staff, err = reader.ReadJSON(path, -1) diff --git a/hw02_fix_app/printer/pkg.go b/hw02_fix_app/printer/pkg.go index 8fa2b33..5ce3f16 100644 --- a/hw02_fix_app/printer/pkg.go +++ b/hw02_fix_app/printer/pkg.go @@ -1,10 +1,12 @@ package printer import ( + "fmt" + "github.com/fixme_my_friend/hw02_fix_app/types" ) -func PrintStaff(staff types.Employee) { +func PrintStaff(staff []types.Employee) { var str string for i := 0; i < len(staff); i++ { str := fmt.Sprintf("User ID: %d; Age: %d; Name: %s; Department ID: %d; ", staff[i].UserID, staff[i].Age, staff[i].Name, staff[i].DepartmentID) diff --git a/hw02_fix_app/reader/pkg.go b/hw02_fix_app/reader/pkg.go index 7adeb97..b774dd0 100644 --- a/hw02_fix_app/reader/pkg.go +++ b/hw02_fix_app/reader/pkg.go @@ -1,12 +1,13 @@ package reader -import "encoding/json" -import "fmt" -import "io" -import "os" - -import "github.com/fixme_my_friend/hw02_fix_app/types" +import ( + "encoding/json" + "fmt" + "io" + "os" + "github.com/fixme_my_friend/hw02_fix_app/types" +) func ReadJSON(filePath string, limit int) ([]types.Employee, error) { f, err := os.Open(filePath) @@ -22,7 +23,7 @@ func ReadJSON(filePath string, limit int) ([]types.Employee, error) { var data []types.Employee - err = json.Unmarshal(bytes, &data) + err = json.Unmarshal(byte, &data) res := data diff --git a/hw02_fix_app/types/employee.go b/hw02_fix_app/types/employee.go index 08b0a04..efb8f80 100644 --- a/hw02_fix_app/types/employee.go +++ b/hw02_fix_app/types/employee.go @@ -10,5 +10,5 @@ type Employee struct { } func (e Employee) String() string { - return fmt.Sprintf("User ID: %d; Age: %d; Name: %s; Department ID: %d; ", e.UserID, e.Age, e.FirstName, e.DepartmentID) + return fmt.Sprintf("User ID: %d; Age: %d; Name: %s; Department ID: %d; ", e.UserID, e.Age, e.Name, e.DepartmentID) } From 8fdc1dc74f6ca79185680ce43405fbf3b559bc63 Mon Sep 17 00:00:00 2001 From: "Arkadiy.Ohotnikov" Date: Wed, 30 Apr 2025 14:00:05 +0300 Subject: [PATCH 2/2] finaly_fix --- hw02_fix_app/main.go | 32 ++++++++++++++++++++++++- hw02_fix_app/printer/pkg.go | 19 ++++++++++++++- hw02_fix_app/reader/pkg.go | 43 ++++++++++++++++++++++++++++++++-- hw02_fix_app/types/employee.go | 21 +++++++++++++++-- 4 files changed, 109 insertions(+), 6 deletions(-) diff --git a/hw02_fix_app/main.go b/hw02_fix_app/main.go index 68db8a0..3cc7d61 100644 --- a/hw02_fix_app/main.go +++ b/hw02_fix_app/main.go @@ -1,5 +1,34 @@ package main +import ( + "fmt" + + "github.com/fixme_my_friend/hw02_fix_app/printer" + "github.com/fixme_my_friend/hw02_fix_app/reader" +) + +func main() { + path := "data.json" + + fmt.Printf("Enter data file path (press Enter to use default): ") + fmt.Scanln(&path) + + if len(path) == 0 { + path = "data.json" + } + + staff, err := reader.ReadJSON(path, -1) + if err != nil { + fmt.Printf("Error reading JSON: %v\n", err) + return + } + + printer.PrintStaff(staff) +} + +/* +package main + import ( "fmt" @@ -9,7 +38,7 @@ import ( ) func main() { - var path string = "data.json" + var path = "data.json" fmt.Printf("Enter data file path: ") fmt.Scanln(&path) @@ -27,3 +56,4 @@ func main() { printer.PrintStaff(staff) } +*/ diff --git a/hw02_fix_app/printer/pkg.go b/hw02_fix_app/printer/pkg.go index 5ce3f16..1d89fd7 100644 --- a/hw02_fix_app/printer/pkg.go +++ b/hw02_fix_app/printer/pkg.go @@ -6,12 +6,29 @@ import ( "github.com/fixme_my_friend/hw02_fix_app/types" ) +func PrintStaff(staff []types.Employee) { + for _, s := range staff { + fmt.Println(s.String()) + } +} + +/* +package printer + +import ( + "fmt" + + "github.com/fixme_my_friend/hw02_fix_app/types" +) + func PrintStaff(staff []types.Employee) { var str string for i := 0; i < len(staff); i++ { - str := fmt.Sprintf("User ID: %d; Age: %d; Name: %s; Department ID: %d; ", staff[i].UserID, staff[i].Age, staff[i].Name, staff[i].DepartmentID) + str := fmt.Sprintf("User ID: %d; Age: %d; Name: %s; Department ID: %d; ", + staff[i].UserID, staff[i].Age, staff[i].Name, staff[i].DepartmentID) fmt.Println(str) } fmt.Println(str) } +*/ diff --git a/hw02_fix_app/reader/pkg.go b/hw02_fix_app/reader/pkg.go index b774dd0..085e33a 100644 --- a/hw02_fix_app/reader/pkg.go +++ b/hw02_fix_app/reader/pkg.go @@ -9,13 +9,51 @@ import ( "github.com/fixme_my_friend/hw02_fix_app/types" ) +func ReadJSON(filePath string, limit int) ([]types.Employee, error) { + f, err := os.Open(filePath) + if err != nil { + return nil, fmt.Errorf("failed to open file: %w", err) + } + defer f.Close() + + bytes, err := io.ReadAll(f) + if err != nil { + return nil, fmt.Errorf("failed to read file: %w", err) + } + + var data []types.Employee + + err = json.Unmarshal(bytes, &data) + if err != nil { + return nil, fmt.Errorf("failed to unmarshal JSON: %w", err) + } + + // если limit >= 0, обрезаем + if limit >= 0 && limit < len(data) { + data = data[:limit] + } + + return data, nil +} + +/*package reader + +import ( + "encoding/json" + "fmt" + "io" + "os" + + "github.com/fixme_my_friend/hw02_fix_app/types" +) + func ReadJSON(filePath string, limit int) ([]types.Employee, error) { f, err := os.Open(filePath) if err != nil { fmt.Printf("Error: %v", err) } - byte, err := io.ReadAll(f) + bytes, err := io.ReadAll(f) if err != nil { fmt.Printf("Error: %v", err) return nil, nil @@ -23,9 +61,10 @@ func ReadJSON(filePath string, limit int) ([]types.Employee, error) { var data []types.Employee - err = json.Unmarshal(byte, &data) + err = json.Unmarshal(bytes, &data) res := data return res, nil } +*/ diff --git a/hw02_fix_app/types/employee.go b/hw02_fix_app/types/employee.go index efb8f80..8a10b4b 100644 --- a/hw02_fix_app/types/employee.go +++ b/hw02_fix_app/types/employee.go @@ -3,12 +3,29 @@ package types import "fmt" type Employee struct { - UserID int `json:"user_id"` + UserID int `json:"userId"` Age int `json:"age"` Name string `json:"name"` - DepartmentID int `json:"department_id"` + DepartmentID int `json:"departmentId"` +} + +func (e Employee) String() string { + return fmt.Sprintf("User ID: %d; Age: %d; Name: %s; Department ID: %d", e.UserID, e.Age, e.Name, e.DepartmentID) +} + +/* +package types + +import "fmt" + +type Employee struct { + UserID int `json:"userId"` + Age int `json:"age"` + Name string `json:"name"` + DepartmentID int `json:"departmentId"` } func (e Employee) String() string { return fmt.Sprintf("User ID: %d; Age: %d; Name: %s; Department ID: %d; ", e.UserID, e.Age, e.Name, e.DepartmentID) } +*/