package main import ( "database/sql" "encoding/json" "net/http" "errors" "fmt" "log" "os" "time" "github.com/shopspring/decimal" _ "github.com/lib/pq" "github.com/jmoiron/sqlx" ) // "json:"json_code_name,omitempty"" (omit empty) // if you use `json:"-"` it doesn't encode it type Transaction struct { Id int `db:"trns_id" json:"Id"` Amount decimal.Decimal `db:"trns_amount" json:"Amount"` Description sql.NullString `db:"trns_description" json:"Description"` Account int `db:"trns_account" json:"Account"` Bucket sql.NullInt64 `db:"trns_bucket" json:"Bucket"` Date time.Time `db:"trns_date" json:"TransactionDate"` } func hello(w http.ResponseWriter, req *http.Request) { fmt.Fprintf(w, "hello\n") } func headers(w http.ResponseWriter, req *http.Request) { for name, headers := range req.Header { for _, h := range headers { fmt.Fprintf(w, "%v: %v\n", name, h) } } } func main() { log.SetPrefix("RecountServer: ") log.SetFlags(0) // db, err := sqlx.Connect("postgres", "user=rcntuser password=Devel@pmentPa$$w0rd host=10.0.0.183 dbname=Borealis sslmode=disable") if err != nil { log.Fatal(err) } log.Print("Database connected") defer db.Close() // Confirm a successful connection. if err := db.Ping(); err != nil { log.Fatal(err) } log.Print("Database successfully pinged") transactions := []Transaction{} err = db.Select(&transactions, "SELECT trns_id, trns_amount, trns_description, trns_account, trns_bucket, trns_date FROM rcnt.transactions ORDER BY trns_id DESC") if err != nil { log.Fatal(err) } fmt.Print(fmt.Sprintf("%+v\n", transactions)) log.Print("Select has been run") for _, trns := range transactions { if trns.Description.Valid { //bytes, err := json.Marshal(trns) bytes, err := json.MarshalIndent(trns, "", "\t") if err != nil { log.Fatal(err) } log.Println(string(bytes)) } else { log.Print("amount invalid") } } log.Print("select all run is done") /* jsonExample := `{ "Id": 3, "Amount": "100", "Description": { "String": "Transaction 3", "Valid": true }, "Account": 1, "Bucket": { "Int64": 1, "Valid": true }, "TransactionDate": "2023-11-11T00:00:00Z" }` var trns Transaction = Transaction{} err = json.Unmarshal([]byte(jsonExample), &trns) if err != nil { log.Println(err) } else { log.Println(trns.Amount) } */ http.HandleFunc("/hello", hello) //http.HandleFunc("/headers", headers) http.ListenAndServe(":8090", nil) if errors.Is(err, http.ErrServerClosed) { fmt.Printf("server closed\n") } else if err != nil { fmt.Printf("error starting server: %s\n", err) os.Exit(1) } //fmt.Println("Hello World") }