Quickstarters
CRUD Samples
How to Build a Basic CRUD App with Golang?
34 min
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 https //www back4app com/docs/get started/new parse app a golang development environment install go https //golang org/doc/install (version 1 16 or later recommended) basic understanding of golang, rest apis, and http servers refer to the golang documentation https //golang org/doc/ if needed step 1 – project setup creating a new back4app project log in to your back4app account click the “new app” button in your dashboard input the project name basic crud app golang and follow the instructions to create your 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 you can add new fields by choosing the data type, naming the field, setting default values, and indicating if it’s required 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 launch the ai agent open your back4app dashboard and locate the ai agent under the project settings describe your data model input a prompt detailing the collections and fields you need review and apply the agent generates the collection definitions review and confirm the changes sample prompt create the following collections in my back4app project 1\) collection items \ fields \ id objectid (auto generated primary key) \ title string \ description string \ created at date (auto generated) \ updated at date (auto updated) 2\) collection users \ fields \ id objectid (auto generated primary key) \ username string (unique) \ email string (unique) \ password hash string \ created at date (auto generated) \ updated at date (auto updated) 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 access the “more” menu on your back4app dashboard select “admin app” and then click “enable admin app ” set up your admin credentials by creating the initial admin user, which also configures roles and system collections enable admin app after activation, log in to the admin app to manage your database 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 initialize your golang project directory create a folder named basic crud app golang and run go mod init basic crud app golang 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 package main import ( 	"encoding/json" 	"fmt" 	"io/ioutil" 	"log" 	"net/http" ) const ( 	appid = "your application id" 	restapikey = "your rest api key" ) type item struct { 	objectid string `json "objectid"` 	title string `json "title"` 	description string `json "description"` 	createdat string `json "createdat"` 	updatedat string `json "updatedat"` } type itemsresponse struct { 	results \[]item `json "results"` } func fetchitems() { 	url = "https //parseapi back4app com/classes/items" 	req, err = http newrequest("get", url, nil) 	if err != nil { 	 log fatalf("error creating request %v", err) 	} 	req header add("x parse application id", appid) 	req header add("x parse rest api key", restapikey) 	client = \&http client{} 	resp, err = client do(req) 	if err != nil { 	 log fatalf("error fetching items %v", err) 	} 	defer resp body close() 	body, err = ioutil readall(resp body) 	if err != nil { 	 log fatalf("error reading response %v", err) 	} 	var itemsresp itemsresponse 	if err = json unmarshal(body, \&itemsresp); err != nil { 	 log fatalf("error parsing json %v", err) 	} 	for , item = range itemsresp results { 	 fmt printf("title %s, description %s\n", item title, item description) 	} } func main() { 	fmt println("fetching items from back4app ") 	fetchitems() } 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 compile your golang code in your project directory, run go build o crud app verify the binary ensure the executable ( crud app ) runs without errors 7 2 organizing your project files your project structure might resemble basic crud app golang/ ├── main go ├── handlers go ├── models go ├── go mod └── readme md 7 3 containerizing with docker if you prefer containerization, include a dockerfile like \# use an official golang image to build the app from golang 1 18 alpine as builder \# set the working directory workdir /app \# copy go mod and go sum files copy go mod go sum / \# download dependencies run go mod download \# copy the source code copy \# build the application run go build o crud app \# use a minimal image to run the app from alpine\ latest workdir /root/ copy from=builder /app/crud app expose 8080 cmd \[" /crud app"] 7 4 deploying your application deploy on your preferred platform whether you choose a vps, a cloud provider, or back4app’s deployment services, upload your binary or docker container monitor the deployment check logs and verify that your api endpoints are reachable 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 https //www back4app com/docs and the golang docs https //golang org/doc/ 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!