-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmain.go
36 lines (31 loc) · 958 Bytes
/
main.go
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
package main
import (
"strconv"
"database/sql"
"fmt"
"os"
"bufio"
"github.com/sohamkamani/go-dependency-injection-example/database"
"github.com/sohamkamani/go-dependency-injection-example/service"
)
func main() {
// Create a new DB connection
connString := "dbname=<your main db name> sslmode=disable"
db, _ := sql.Open("postgres", connString)
// Create a store dependency with the db connection
store := database.NewStore(db)
// Create the service by injecting the store as a dependency
service := &service.Service{Store: store}
// The following code implements a simple command line app to read the ID as input
// and output the validity of the result of the entry with that ID in the database
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan(){
ID, _ := strconv.Atoi(scanner.Text())
err := service.GetNumber(ID)
if err != nil {
fmt.Printf("result invalid: %v", err)
continue
}
fmt.Println("result valid")
}
}