44 lines
778 B
Markdown
44 lines
778 B
Markdown
|
# point
|
||
|
|
||
|
This sample module shows how to create a type that implements the required interfaces to be used as a custom type with [github.com/jmoiron/sqlx](github.com/jmoiron/sqlx) package.
|
||
|
|
||
|
Sample db table:
|
||
|
|
||
|
```
|
||
|
CREATE TABLE places (id SERIAL, name varchar(512), loc point);
|
||
|
```
|
||
|
|
||
|
and then use as such (err omitted for brevity):
|
||
|
|
||
|
|
||
|
```
|
||
|
type Location struct {
|
||
|
Id int
|
||
|
Name string
|
||
|
Description string
|
||
|
Loc Point // custom type
|
||
|
}
|
||
|
|
||
|
func main() {
|
||
|
db, _ := sqlx.Connect(
|
||
|
"postgres",
|
||
|
"<your config here>",
|
||
|
)
|
||
|
|
||
|
loc := Location{}
|
||
|
rows, _ := db.Queryx(
|
||
|
`SELECT * FROM places`,
|
||
|
)
|
||
|
for rows.Next() {
|
||
|
rows.StructScan(&loc)
|
||
|
fmt.Printf("%+v", loc)
|
||
|
}
|
||
|
|
||
|
db.Exec(
|
||
|
"INSERT INTO places(name, loc) VALUES ($1, $3):,
|
||
|
"some name"
|
||
|
Point{3.14, 6.28},
|
||
|
)
|
||
|
}
|
||
|
```
|