Comments (6)
For what it's worth, in 9f9a977 I added compatibility with the standard database/sql.Scanner and database/sql/driver.Valuer interfaces for custom types. This means you can use https://github.com/shopspring/decimal as a type that maps to PostgreSQL numeric. See https://github.com/jackc/pgx/blob/master/query_test.go#L913 for an example.
from pgx.
Unfortunately, there is no decimal type in the Go standard library so numeric columns will be returned as text. It is relatively simple to create custom type mappings (https://github.com/jackc/pgx/blob/master/example_custom_type_test.go) once you've chosen a type to represent it in Go.
For the JSONB type it should be returned as a string by default.
from pgx.
Well, it's not a problem specific to Go, but you probably don't want to use floats to represent money.
http://spin.atomicobject.com/2014/08/14/currency-rounding-errors/
It's unfortunate that Go doesn't have a decimal type in its standard library. I haven't had to deal with money in my Go applications yet, so I don't have a good suggestion for a 3rd party decimal library, but there may be one. Another option is to convert to integers and there money as the number of cents.
from pgx.
Thanks. I think that custom type mapping code is a bit out my reach as a newbie Gopher :)
I just did a conversion to float64 and it worked fine (I got my $27.95 Postgres decimal value):
product.Price, err = strconv.ParseFloat(sPrice, 64)
Are there any issues with this? Is this the common practice for working with Postgres datatypes not supported in Go?
from pgx.
Thanks for the tip. I may as well use an int64 in both Postgres and the Go model to store and calculate the product price in cents and only add the dot when displaying the price in the browser. There may even be a small performance advantage for using integers...
from pgx.
Thanks for adding this! It's going to majorly help me out on this latest project(financial-related).
from pgx.
Related Issues (20)
- Interval of 0s is invalid HOT 4
- Add LoadTypes method for bulk type registration HOT 2
- Scan not only anonymous structs HOT 4
- Do not close the connection when the error does not indicate that the connection is broken. HOT 1
- Memory leak in scanPlanString HOT 1
- Support for connections/connstrings that don't require passwords HOT 1
- When using stdlib, Rollback is not sent to DB when transaction context times out HOT 2
- Convert pgtype.Interval to time.Duration HOT 3
- Tracelog uses "time" when logging which can cause issues with JSON payloads HOT 2
- Panic on invalid SSL Key File
- Create `pgtype.Timestamptz` from `time.Time` HOT 3
- `pgtype.Int8.Scan` compares an `int64` with `math.MinInt64` HOT 1
- No error returned from Query (Exec works) HOT 1
- change of tenant for multi tenancy using pgxpool
- Conform UUID to Stringer interface HOT 4
- WaitCount stats HOT 1
- How can we tell if pgx.Rows don't get closed? HOT 2
- Support libpq compatible load_balance_hosts parameter HOT 4
- Cannot work with custom domain based on JSON/JSONB HOT 2
- Option to wrap goroutines with a custom handler HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pgx.