Hello folks... Documentation for the Go is hard to find in a simple way for beginners. I hope that with these posts I can help many people who are starting out in the world of Go. In a later project we will use NEXTJS to consume data from this API. π
Installing Go
1ΒΊ Step: Go to https://go.dev/doc/install and choose the file for your operation system. Make the download em double click in file. Follow the steps.
How do I start?
1st Step: Create a new folder on your PC. Let's call it, for example, "my-first-go-api"
2nd Step: In VS Code, open your folder and initialize your Go module with the command line below in your terminal.
go mod init api-with-go-postgres
3rd Step: Now, in your folder, you need to create the structure with the following blank files below:
api-with-go-postgres/
βββ database/
β βββ connection.go
βββ products/
β βββ create_product.go
β βββ delete_product_by_id.go
β βββ update_product_by_id.go
β βββ get_all_products.go
βββ auth/
β βββ forgot_password.go
β βββ login.go
β βββ reset_password.go
βββ main.go
4th Step: Open all the files in VSCODE and put the code below in each file:
database/connection.go file
package database // the same name in your folder
import (
"fmt"
)
func Connection() {
fmt.Println("Connection logic here...")
}
products/create_product.go file
package products // the same name in your folder
import (
"fmt"
)
func CreateProduct() {
fmt.Println("Create Product logic here...")
}
products/delete_product_by_id.go file
package products // the same name in your folder
import (
"fmt"
)
func DeleteProductById() {
fmt.Println("Delete Product By Id logic here...")
}
products/update_product_by_id.go file
package products // the same name in your folder
import (
"fmt"
)
func UpdateProductById() {
fmt.Println("Update Product By Id logic here...")
}
products/get_all_products.go file
package products // the same name in your folder
import (
"fmt"
)
func GetAllProducts() {
fmt.Println("Get All Products logic here...")
}
5th Step: Now following the same logic, and do the same for the files inside the auth folder, but so this post doesn't get long I won't put the codes here :-)
6th Step: Now let's call the functions within the main.go file to see if the program can recognize all the functions created so far:
package main
import (
"api-with-go-postgres/auth"
"api-with-go-postgres/database"
"api-with-go-postgres/products"
)
func main() {
database.Connection()
auth.Login()
auth.ForgotPassword()
auth.ResetPassword()
products.CreateProduct()
products.DeleteProductById()
products.UpdateProductById()
products.GetAllProducts()
}
7th Step: Now run the code below to execute our GO program:
go run main.go
8th Step: If everything went well and you followed the steps correctly you should see the following information in the terminal:
Connection logic here...
Login logic here...
Forgot Password logic here...
Reset Password logic here...
Create Product logic here...
Delete Product By Id logic here...
Update Product By Id logic here...
Get All Products logic here...
So that's it for today folks. Follow the next steps here. We will learn how to create large, complete projects using Javascript technology for the Front-End and GoLang technology for the Back-End.
Until next time!
GIT REPO: https://github.com/phantodev/api-with-go-postgres
BRANCH: step-1