ProgramGuru

CRUD Operations

Start with CRUD Operations

In this lesson, explore crud operations through one small Go program, and connect its output to the language rules behind it.

What you’ll learn

  • Explain the main purpose of crud operations.
  • Read and run the complete Go example in this lesson.
  • Apply the concept and recognize common mistakes.

Understand the idea

CRUD stands for create, read, update and delete. A task API may POST to create a task, GET to read it, PATCH to change fields and DELETE to remove it.

For SQL storage, use parameterized INSERT, SELECT, UPDATE and DELETE statements. This runnable program demonstrates the business effects with an in-memory map; it does not persist or execute SQL.

Visualize how it works

Follow this diagram before executing the example. Check where a value or decision changes what runs next.

Visual explanation for crudCRUD describes changes to resource state.Create recordStep 1Read recordStep 2Update recordStep 3Delete recordStep 4CRUD describes changes to resource state.
CRUD describes changes to resource state.

Tools, syntax and practical notes

Parameterized CRUD SQL

INSERT INTO tasks (id, title) VALUES (?, ?);
SELECT id, title FROM tasks WHERE id = ?;
UPDATE tasks SET title = ? WHERE id = ?;
DELETE FROM tasks WHERE id = ?;

For PostgreSQL, substitute the driver’s positional placeholder syntax. Execute these with ExecContext or QueryRowContext as appropriate.

Run a complete Go example

This example is a complete program. Save it as main.go or paste it into the Go Playground.

Go code

package main

import "fmt"

func main() {
	tasks := map[int]string{}
	tasks[1] = "Write Go" // create
	fmt.Println("Read:", tasks[1])
	tasks[1] = "Learn Go" // update
	fmt.Println("Updated:", tasks[1])
	delete(tasks, 1) // delete
	_, ok := tasks[1]
	fmt.Println("Exists:", ok)
}

Output

Read: Write Go
Updated: Learn Go
Exists: false

Trace what the program does

The map first stores, then reads, updates and deletes task 1. It prints a missing record after deletion. A SQL-backed repository would map these operations to statements and durable outcomes.

Avoid common mistakes

  • A successful SQL UPDATE may affect zero rows; inspect RowsAffected when needed.
  • CRUD permissions are authorization decisions, not just HTTP methods.

Try it yourself

Write SQL statements for the four task operations and list expected status codes.

Tip: Edit the example above, predict the output, and run the modified code to test your reasoning.

Check your understanding

What does U represent in CRUD?
Choose one answer. You can try again.

Answer and explanation

Update

Correct. The map first stores, then reads, updates and deletes task 1.

What you should remember

You have practiced CRUD Operations in a small, self-contained program. The important habit is to predict what runs, inspect the actual output and distinguish the language rule from the particular example.

Before continuing, make sure you can explain the program to someone who has not seen it, including the line that changes its result.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Prepared Statements →