Simple api in golang using gin and docker.
- Golang
- Gin
go mod tidy
go mod run main.go
go build main.go
./main
docker build -t go-basic-api .
docker-compose up -d go-basic-db
docker-compose up -d go-api-app
docker container ls
docker exec -it go-basic-db psql -U postgres -d code043
psql (12.20 (Debian 12.20-1.pgdg120+1)) \
Type "help" for help.
code043=#
create table product (
id serial primary key,
product_name varchar(50) not null,
price numeric(10, 2) not null
);psql (12.20 (Debian 12.20-1.pgdg120+1)) \
Type "help" for help.
code043=#create table product (
id serial primary key,
product_name varchar(50) not null,
price numeric(10, 2) not null
);
insert into product (product_name, price) values('Coffee', 20);
psql (12.20 (Debian 12.20-1.pgdg120+1)) \
Type "help" for help.
code043=#insert into product (product_name, price) values('Coffee', 20);
docker logs -f go-api-app
curl http://localhost:8000/products
output: [{"id_product":1,"name":"Coffee","price":20}]
get http://localhost:8000/products
[
{
id_product: 1,
name: "Coffee",
price: 20,
},
];-
Vscode:
-
Golang:
- install: https://go.dev/doc/install
-
Gin:
-
Docker:
-
Rest Client: