Lessons
Go Introduction
Go Get Started
Go Syntax
Go Comments
Go Variables
- Declare Variables
- Go में Variables का परिचय
- Go में Multiple Variable Declaration
- Variable Naming Rules (नामकरण के नियम)
Go Constants
Go Output
Go Data Types
- Basic Data Types
- Go Boolean Data Type (बूलियन डेटा प्रकार)
- Go Integer Data Types
- Go Float Data Types
- Go String Data Type
Go Arrays
Go Slices
Go Operators
- Go Operators
- Arithmetic Operators
- Assignment Operators
- Comparison Operators
- Logical Operators
- Bitwise Operators
Go Conditions
Go Switch
Go Loops
Go Functions
Go Struct
Go Maps
Project Structure
Explain Project Structure
Go CRUD Project Documentation
Ye documentation Go language aur PostgreSQL CRUD project ke liye hai. Sabhi code examples aur explanations Hindi me diye gaye hain.
1. Project Structure
go-postgres-crud/
├── go.mod
├── main.go
├── db/
│ └── db.go
├── models/
│ └── user.go
├── handlers/
│ └── user_handler.go
├── routes/
│ └── routes.go
└── tests/
└── user_test.go
2. PostgreSQL Setup
CREATE DATABASE techdeepak;
CREATE TABLE users (
id SERIAL PRIMARY KEY,
name VARCHAR(100),
email VARCHAR(100) UNIQUE NOT NULL
);
3. Go Modules Initialize
go mod init go-postgres-crud
go get github.com/lib/pq
go get github.com/gorilla/mux
go get github.com/stretchr/testify
4. Database Connection (db/db.go)
package db
import (
"database/sql"
"fmt"
_ "github.com/lib/pq"
)
var DB *sql.DB
func Connect() {
connStr := "user=postgres password=yourpassword dbname=techdeepak sslmode=disable"
database, err := sql.Open("postgres", connStr)
if err != nil {
panic(err)
}
err = database.Ping()
if err != nil {
panic(err)
}
fmt.Println("Database Connected Successfully")
DB = database
}
5. Model (models/user.go)
package models
type User struct {
ID int `json:"id"`
Name string `json:"name"`
Email string `json:"email"`
}
6. Handlers (handlers/user_handler.go)
package handlers
import (
"encoding/json"
"fmt"
"net/http"
"strconv"
"github.com/gorilla/mux"
"go-postgres-crud/db"
"go-postgres-crud/models"
)
// CREATE USER
func CreateUser(w http.ResponseWriter, r *http.Request) { ... }
// READ ALL USERS
func GetUsers(w http.ResponseWriter, r *http.Request) { ... }
// READ SINGLE USER
func GetUser(w http.ResponseWriter, r *http.Request) { ... }
// UPDATE USER
func UpdateUser(w http.ResponseWriter, r *http.Request) { ... }
// DELETE USER
func DeleteUser(w http.ResponseWriter, r *http.Request) { ... }
7. Routes (routes/routes.go)
package routes
import (
"github.com/gorilla/mux"
"go-postgres-crud/handlers"
)
func SetupRoutes() *mux.Router {
router := mux.NewRouter()
router.HandleFunc("/users", handlers.CreateUser).Methods("POST")
router.HandleFunc("/users", handlers.GetUsers).Methods("GET")
router.HandleFunc("/users/{id}", handlers.GetUser).Methods("GET")
router.HandleFunc("/users/{id}", handlers.UpdateUser).Methods("PUT")
router.HandleFunc("/users/{id}", handlers.DeleteUser).Methods("DELETE")
return router
}
8. Main File (main.go)
package main
import (
"fmt"
"net/http"
"go-postgres-crud/db"
"go-postgres-crud/routes"
)
func main() {
db.Connect()
router := routes.SetupRoutes()
fmt.Println("Server running on port 8080")
http.ListenAndServe(":8080", router)
}
9. Test Cases (tests/user_test.go)
package tests
import (
"bytes"
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"go-postgres-crud/handlers"
"go-postgres-crud/models"
)
func TestCreateUser(t *testing.T) { ... }
func TestGetUsers(t *testing.T) { ... }
Summary
Database Connection: db/db.go me PostgreSQL connect kiya.
Model: User struct banaya JSON aur DB mapping ke liye.
Handlers: CRUD operations ke liye HTTP handlers.
Routes: Gorilla Mux router setup kiya.
Main: Server start aur routes register.
Tests: httptest aur testify use kiya unit tests ke liye.