Skip to content

Commit

Permalink
hw 2 done
Browse files Browse the repository at this point in the history
  • Loading branch information
Олег Владимирович Евдокимов committed Dec 21, 2023
1 parent 19855f4 commit ee644f3
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 16 deletions.
7 changes: 4 additions & 3 deletions hw02_fix_app/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ func main() {
path = "data.json"
}

staff, err = reader.ReadJSON(path)

fmt.Print(err)
staff, err = reader.ReadJSON(path, -1)

if err != nil {
fmt.Print(err)
}
printer.PrintStaff(staff)
}
9 changes: 1 addition & 8 deletions hw02_fix_app/printer/pkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,7 @@ import (
)

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,
)
fmt.Println(str)
fmt.Println(staff[i])
}

// fmt.Println(str)
}
9 changes: 4 additions & 5 deletions hw02_fix_app/reader/pkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,22 @@ import (
"github.com/es-x/go_basic/hw02_fix_app/types"
)

func ReadJSON(filePath string) ([]types.Employee, error) {
func ReadJSON(filePath string, _ int) ([]types.Employee, error) {
f, err := os.Open(filePath)
if err != nil {
fmt.Printf("Error: %v", err)
return nil, err
}

bytes, err := io.ReadAll(f)
if err != nil {
fmt.Printf("Error: %v", err)
return nil, nil
return nil, err
}

var data []types.Employee

json.Unmarshal(bytes, &data)

res := data

return res, nil
return data, nil
}

0 comments on commit ee644f3

Please sign in to comment.