-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuserService.go
More file actions
38 lines (31 loc) · 897 Bytes
/
Copy pathuserService.go
File metadata and controls
38 lines (31 loc) · 897 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package main
import (
"database/sql"
"encoding/json"
"fmt"
"go_with_sql/repository"
)
func printUsers(connection *sql.DB) {
fmt.Print("*** [postgres] Printing users...")
users := repository.GetUsers(connection)
data := ToJson(users)
fmt.Printf("#### All > %s\n", data)
user := repository.GetUserById(connection, 4)
data = ToJson(user)
fmt.Printf("### ById > %s\n", data)
user = repository.GetUserByUsername(connection, "jane")
data = ToJson(user)
fmt.Printf("## ByUsername > %s\n", data)
updated := repository.UpdateUser(connection, "Jane Doe", user.Id)
fmt.Printf("# NameUpdated > %v\n", updated)
defer func(connection *sql.DB) {
err := connection.Close()
repository.CheckError(err)
}(connection)
}
// ToJson This is generics like in java
func ToJson[D any](d D) string {
marshaled, err := json.MarshalIndent(d, "", " ")
checkError(err)
return string(marshaled)
}