REST API with Golang and MongoDB

Dependencies

Install all required dependencies:

go get -u github.com/gin-gonic/gin go.mongodb.org/mongo-driver/mongo github.com/joho/godotenv github.com/go-playground/validator/v10

API:

Create user:

curl --location --request POST 'http://localhost:8080/users/' \
--header 'Content-Type: application/json' \
--data-raw '{
    "name": "Marco Reus",
    "location": "Sweden",
    "title": "Software Engineer"
}'

Get user by id:

curl --location --request GET 'http://localhost:8080/users/63c03f81cfba3a189540572b'

Update user info:

curl --location --request PUT 'http://localhost:8080/users/63c03f81cfba3a189540572b' \
--header 'Content-Type: application/json' \
--data-raw '{
    "name": "Marco Reus!",
    "location": "Sweden",
    "title": "Go Software Engineer"
}'

Delete user:

curl --location --request DELETE 'http://localhost:8080/users/63c03f81cfba3a189540572b'

github.com/gin-gonic/gin is a framework for building web applications.

go.mongodb.org/mongo-driver/mongo is a driver for connecting to MongoDB.

github.com/joho/godotenv is a library for managing environment variables.

github.com/go-playground/validator/v10 is a library for validating structs and fields.

You may find these resources helpful: