Skip to main content

Command Palette

Search for a command to run...

GoLang API + Postgres for Beginners: Step 1

Updated
β€’3 min read
GoLang API + Postgres for Beginners: Step 1
C

Eduardo Burko, aka Phantodev, i'm a skilled front-end developer with a focus on creating robust applications using the React framework. With extensive experience in implementing complex features, i create clean and organized code, ensuring his applications are scalable and easy to maintain. I'm a quick learner and proficient in other front-end technologies like HTML, CSS, and JavaScript, and back-end technologies like Node.js and Express. I have experience working with clients in various industries, developing custom solutions for them. I'm strength in communication and working in agile environments makes me flexible and adaptable to changing project requirements.

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

Eduardo Burko - Fullstack Developer

Get in touch in phantodev.com.br

More from this blog

Show me the code, Phantodev!

12 posts

I'm working with Front-End since to 2005 encompassing software development, architecture and UI Design. Get in Touch and let's work together? I'm open to work in new projects :-)