Quickstarters

How to Build a Basic CRUD App with Golang?

34min

Introduction

In this guide, you'll learn how to develop a fundamental CRUD (create, read, update, and delete) application using Golang.

This tutorial covers the essential operations needed to manage data effectively, leveraging Back4app as a robust backend service. You will now build a Golang server that communicates with Back4app via RESTful calls.

Starting off, you'll create and configure a Back4app project named Basic-CRUD-App-Golang. This project will serve as the central database for your application.

Next, you’ll define a scalable database schema by establishing detailed collections and fields—either manually or with the help of the Back4app AI Agent. This setup ensures your system is primed for efficient data manipulation through CRUD operations.

Following that, you will activate the Back4app Admin App, a user-friendly tool with drag-and-drop capabilities to manage your collections. This simplifies the creation, reading, updating, and deletion of records.

Finally, you'll integrate your Golang backend with Back4app using REST APIs, while also implementing robust security controls and user authentication where necessary.

By the end of this tutorial, you'll have a production-ready Golang web server that supports basic CRUD operations and connects seamlessly with a Back4app-managed database.

Key Takeaways

  • Understand how to construct CRUD applications with Golang and manage data using Back4app.
  • Gain insights into setting up a scalable backend architecture and integrating it with a Golang REST API.
  • Learn to use the Back4app Admin App for hassle-free data management.
  • Discover deployment techniques including containerization for quick launches.


Prerequisites

Before starting, ensure you have the following:

  • A Back4app account with a new project set up. For guidance, check Getting Started with Back4app.
  • A Golang development environment. Install Go (version 1.16 or later recommended).
  • Basic understanding of Golang, REST APIs, and HTTP servers. Refer to the Golang documentation if needed.


Step 1 – Project Setup

Creating a New Back4app Project

  1. Log in to your Back4app account.
  2. Click the “New App” button in your dashboard.
  3. Input the project name: Basic-CRUD-App-Golang and follow the instructions to create your project.
Create New Project
Create New Project


After the project is set up, you will see it listed on your Back4app dashboard, ready to support your backend configuration.



Step 2 – Database Schema Design

Defining Your Data Structure

For a basic CRUD application, you’ll create several collections. Here are examples of the collections and fields you might need:

1. Items Collection

This collection stores details about each item.

Field

Data Type

Description

_id

ObjectId

Auto-generated primary key.

title

String

The name or title of the item.

description

String

A concise description of the item.

created_at

Date

Timestamp marking item creation.

updated_at

Date

Timestamp marking the last update.

2. Users Collection

This collection retains user data and authentication details.

Field

Data Type

Description

_id

ObjectId

Auto-generated primary key.

username

String

Unique identifier for the user.

email

String

User's unique email address.

password_hash

String

Encrypted password for login.

created_at

Date

Timestamp when the account was created.

updated_at

Date

Timestamp when the account was last updated.

You can manually set up these collections via the Back4app dashboard by creating a new class for each and defining the columns.

Create New Class
Create New Class


You can add new fields by choosing the data type, naming the field, setting default values, and indicating if it’s required.

Create Column
Create Column



Leveraging the Back4app AI Agent for Schema Creation

The Back4app AI Agent, accessible via your dashboard, can automatically create your database schema from a descriptive prompt. This feature streamlines project setup and ensures consistency.

How to Use the AI Agent:

  1. Launch the AI Agent: Open your Back4app dashboard and locate the AI Agent under the project settings.
  2. Describe Your Data Model: Input a prompt detailing the collections and fields you need.
  3. Review and Apply: The agent generates the collection definitions. Review and confirm the changes.

Sample Prompt

Text


This method saves time and helps ensure your schema is robust and optimized.



Step 3 – Activating the Admin App & Performing CRUD Operations

Overview of the Admin App

The Back4app Admin App is an intuitive, no-code interface that allows you to manage your backend data. It provides a drag-and-drop interface for creating, reading, updating, and deleting records.

Enabling the Admin App

  1. Access the “More” menu on your Back4app dashboard.
  2. Select “Admin App” and then click “Enable Admin App.”
  3. Set up your admin credentials by creating the initial admin user, which also configures roles and system collections.
Enable Admin App
Enable Admin App


After activation, log in to the Admin App to manage your database.

Admin App Dashboard
Admin App Dashboard


Using the Admin App for CRUD Tasks

Within the Admin App, you can:

  • Create Records: Use the “Add Record” button in a collection (e.g., Items) to insert new data.
  • Read/Update Records: Click on a record to view or modify its details.
  • Delete Records: Remove records that are obsolete with the delete option.

This interface simplifies data management with an intuitive drag-and-drop design.



Step 4 – Integrating Golang with Back4app

Now that your backend is configured and managed via the Admin App, it’s time to connect your Golang application to Back4app using RESTful endpoints.

Setting Up Your Golang Project

  1. Initialize your Golang project directory: Create a folder named basic-crud-app-golang and run:

    Bash
    
  2. Install necessary packages: For this tutorial, the standard library suffices, but you may choose to add external libraries as needed.

Sample Code to Fetch and Display Items

Below is an example of a Golang program that retrieves items from your Back4app Items collection:

Go


This program sends a GET request to the Back4app Items endpoint, decodes the JSON response, and prints out each item's title and description.

Extending the CRUD Functionality

You can similarly implement functions in Golang for:

  • Creating an item: Send a POST request with item data.
  • Updating an item: Use a PUT or POST request to modify existing records.
  • Deleting an item: Issue a DELETE request to remove records.

Integrate these functions into a full HTTP server using Go's net/http package to handle routes for your CRUD operations.



Step 5 – Securing Your Backend

Implementing Access Controls

Enhance your data security by applying Access Control Lists (ACLs) when performing CRUD operations.

For instance, ensure that only authorized users can modify or delete data by setting appropriate ACLs through Back4app’s dashboard and enforcing them via your Golang API.

Configuring Class-Level Permissions

Adjust Class-Level Permissions (CLPs) in the Back4app interface to restrict data access to authenticated or specific roles.



Step 6 – User Authentication

Setting Up Account Management

Back4app uses Parse’s User class for authentication. In your Golang application, handle user registration and login by sending requests to the appropriate Back4app endpoints. This can include:

  • User Registration: A POST request with username, email, and password.
  • User Login: Verifying credentials and managing session tokens.

Integrate these endpoints within your Golang server to secure your CRUD operations.



Step 7 – Deploying Your Golang Application

7.1 Preparing Your Production Build

  1. Compile your Golang code: In your project directory, run:

    Bash
    
  2. Verify the binary: Ensure the executable (crud-app) runs without errors.

7.2 Organizing Your Project Files

Your project structure might resemble:

Text


7.3 Containerizing with Docker

If you prefer containerization, include a Dockerfile like:

Dockerfile


7.4 Deploying Your Application

  1. Deploy on your preferred platform: Whether you choose a VPS, a cloud provider, or Back4app’s deployment services, upload your binary or Docker container.
  2. Monitor the deployment: Check logs and verify that your API endpoints are reachable.
  3. Test the Application: Access your server’s URL to confirm that all CRUD endpoints operate as intended.


Step 8 – Conclusion and Next Steps

Great job! You’ve successfully built a basic CRUD application using Golang and Back4app. You set up a project called Basic-CRUD-App-Golang, designed collections for Items and Users, and connected your Golang backend to Back4app via REST APIs.

Next Steps:

  • Enhance Your API: Add more endpoints for detailed item views, search capabilities, or real-time updates.
  • Implement Additional Security: Explore advanced authentication methods and further secure your endpoints.
  • Explore Further Resources: Consult the Back4app documentation and the Golang docs for more in-depth insights.

By following this tutorial, you now have the foundation to build robust, scalable CRUD applications with Golang, integrated seamlessly with Back4app. Happy coding!