muslchn/go-pegawai
A modular Go application for managing employee data with full CRUD operations, salary calculations, and SQLite integration using GORM.
๐ข Go Pegawai - Employee Management System
A comprehensive employee management application built with Go and GORM, demonstrating full CRUD operations with MariaDB database integration, environment-based configuration, and enterprise-grade best practices.
๐ Table of Contents
- Features
- Tech Stack
- Prerequisites
- Installation
- Usage
- Project Structure
- Database Schema
- API Documentation
- Examples
- Contributing
- License
โจ Features
- Complete CRUD Operations: Create, Read, Update, and Delete employee records
- MariaDB Integration: Production-ready MariaDB database with GORM ORM
- Environment Configuration: Secure configuration management with environment variables
- Connection Pooling: Optimized database connections for performance
- Annual Salary Calculation: Automatic calculation of yearly salary (12 ร monthly salary)
- Data Persistence: MariaDB database for reliable, scalable data storage
- Soft Deletes: Safe deletion with GORM's built-in soft delete functionality
- Clean Architecture: Organized code structure with separate packages
- Error Handling: Comprehensive error handling for all operations
- Security Best Practices: Environment variables for sensitive data
- Console Interface: User-friendly console output with formatted display
๐ Tech Stack
- Language: Go 1.21+
- ORM: GORM v1.31.0
- Database: MariaDB/MySQL
- Database Driver: MySQL Driver v1.8.1
- Configuration: godotenv v1.5.1 for environment variable management
๐ Prerequisites
Before running this application, make sure you have the following installed:
๐ Installation
-
Clone the repository
git clone https://github.com/muslchn/go-pegawai.git cd go-pegawai -
Install dependencies
go mod tidy
-
Set up MariaDB database
-- Connect to MariaDB as root mysql -u root -p -- Create database CREATE DATABASE pegawai_db CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; -- Create user (optional, for security) CREATE USER 'pegawai_user'@'localhost' IDENTIFIED BY 'your_secure_password'; GRANT ALL PRIVILEGES ON pegawai_db.* TO 'pegawai_user'@'localhost'; FLUSH PRIVILEGES;
-
Configure environment variables
Copy the example environment file and update with your database credentials:
cp .env.example .env
Edit
.envfile with your database configuration:# Database Configuration DB_HOST=localhost DB_PORT=3306 DB_USERNAME=root DB_PASSWORD=your_password DB_NAME=pegawai_db # Application Configuration APP_PORT=8080 APP_ENV=development # Database Connection Settings DB_MAX_OPEN_CONNS=25 DB_MAX_IDLE_CONNS=10 DB_CONN_MAX_LIFETIME=300
-
Run the application
go run main.go
๐ป Usage
The application runs a demonstration of all CRUD operations automatically:
go run main.goApplication Flow
- Configuration Loading: Loads environment variables from
.envfile - Database Initialization: Connects to MariaDB and migrates schema
- CREATE: Adds 5 sample employees to the database
- READ: Displays all employees with salary calculations
- UPDATE: Modifies an employee's salary and shows the changes
- DELETE: Removes an employee and displays updated list
- SUMMARY: Shows final employee list with statistics
๐ Project Structure
go-pegawai/
โโโ main.go # Main application entry point
โโโ go.mod # Go module definition
โโโ go.sum # Go module checksums
โโโ .env # Environment variables (not in git)
โโโ .env.example # Environment variables template
โโโ .gitignore # Git ignore file
โโโ config/ # Configuration package
โ โโโ config.go # Environment variable loading and config management
โโโ pegawai/ # Employee package
โ โโโ data.go # Employee struct and CRUD operations
โโโ README.md # Project documentation
๐ Database Schema
Pegawai Table (MariaDB)
| Column | Type | Constraints | Description |
|---|---|---|---|
| id | BIGINT | PRIMARY KEY, AUTO_INC | Unique employee identifier |
| created_at | DATETIME(3) | NOT NULL | Record creation timestamp |
| updated_at | DATETIME(3) | NOT NULL | Record last update timestamp |
| deleted_at | DATETIME(3) | NULL | Soft delete timestamp |
| nama | VARCHAR(100) | NOT NULL | Employee full name |
| posisi | VARCHAR(100) | NOT NULL | Employee position/role |
| gaji_bulanan | DECIMAL(15,2) | NOT NULL | Monthly salary amount |
Database Configuration
- Character Set: utf8mb4
- Collation: utf8mb4_unicode_ci
- Engine: InnoDB (default)
- Connection Pool: Configurable via environment variables
๐ API Documentation
Core Functions
Database Operations
// Initialize database connection
func InitDatabase() error
// Create new employee
func CreatePegawai(pegawai *Pegawai) error
// Get all employees
func GetAllPegawai() ([]Pegawai, error)
// Get employee by ID
func GetPegawaiByID(id uint) (*Pegawai, error)
// Update employee salary
func UpdatePegawaiSalary(id uint, newSalary float64) error
// Delete employee (soft delete)
func DeletePegawai(id uint) error
// Display all employees with formatting
func DisplayAllPegawai() errorConfiguration Management
// Load configuration from environment variables
func LoadConfig() (*Config, error)
// Get current configuration instance
func GetConfig() *Config
// Configuration structures
type Config struct {
Database DatabaseConfig
App AppConfig
}
type DatabaseConfig struct {
Host string
Port int
Username string
Password string
Name string
MaxOpenConns int
MaxIdleConns int
ConnMaxLifetime int
}
type AppConfig struct {
Port string
Env string
}Employee Methods
// Calculate annual salary
func (p Pegawai) HitungGajiTahunan() float64
// Display employee information
func (p Pegawai) TampilkanInformasi()Data Structures
type Pegawai struct {
gorm.Model // ID, CreatedAt, UpdatedAt, DeletedAt
Nama string `gorm:"size:100;not null" json:"nama"`
Posisi string `gorm:"size:100;not null" json:"posisi"`
GajiBulanan float64 `gorm:"not null" json:"gaji_bulanan"`
}๐ Examples
Sample Output
๐ข APLIKASI MANAJEMEN DATA PEGAWAI ๐ข
=====================================
๐ง DEMO OPERASI CRUD PEGAWAI
============================
๐ STEP 1: MENAMBAH 5 DATA PEGAWAI
----------------------------------
โ
Pegawai berhasil ditambahkan dengan ID: 1
โ
Pegawai berhasil ditambahkan dengan ID: 2
...
๐ STEP 2: MENAMPILKAN SEMUA DATA PEGAWAI
------------------------------------------
MariaDB database connected successfully to localhost:3306/pegawai_db
Database schema migrated successfully
๐ค PEGAWAI 1:
=== INFORMASI PEGAWAI ===
ID: 1
Nama: Ahmad Wijaya
Posisi: Software Engineer
Gaji Bulanan: Rp 8,500,000.00
Gaji Tahunan: Rp 102,000,000.00
========================
Sample Employee Data
| ID | Name | Position | Monthly Salary | Annual Salary |
|---|---|---|---|---|
| 1 | Ahmad Wijaya | Software Engineer | Rp 8,500,000 | Rp 102,000,000 |
| 2 | Siti Nurhaliza | Product Manager | Rp 12,000,000 | Rp 144,000,000 |
| 3 | Budi Santoso | UI/UX Designer | Rp 7,500,000 | Rp 90,000,000 |
| 4 | Rina Marlina | Data Analyst | Rp 9,000,000 | Rp 108,000,000 |
| 5 | Doni Prasetyo | DevOps Engineer | Rp 10,500,000 | Rp 126,000,000 |
๐ Key Features Demonstrated
CRUD Operations
- โ Create: Insert 5 employees with complete information
- โ Read: Retrieve and display all employee data
- โ Update: Modify employee salary with before/after comparison
- โ Delete: Remove employee record with confirmation
Business Logic
- โ Annual Salary Calculation: 12 ร monthly salary
- โ Data Validation: GORM constraints and error handling
- โ Soft Deletes: Safe record removal
- โ Statistics: Total and average salary calculations
๐ก Error Handling
The application includes comprehensive error handling for:
- Database connection failures
- Environment variable loading errors
- Record not found scenarios
- Invalid data constraints
- SQL operation errors
- Configuration validation errors
๐ง Configuration
Environment Variables
The application uses environment variables for configuration. Copy .env.example to .env and configure:
# Database Configuration
DB_HOST=localhost # Database host
DB_PORT=3306 # Database port
DB_USERNAME=root # Database username
DB_PASSWORD=your_password # Database password
DB_NAME=pegawai_db # Database name
# Application Configuration
APP_PORT=8080 # Application port
APP_ENV=development # Environment (development/production)
# Database Connection Settings
DB_MAX_OPEN_CONNS=25 # Maximum open connections
DB_MAX_IDLE_CONNS=10 # Maximum idle connections
DB_CONN_MAX_LIFETIME=300 # Connection lifetime in secondsSecurity Best Practices
- โ Environment variables for sensitive data
- โ
.envfile excluded from version control - โ
.env.exampletemplate for setup - โ Database connection pooling
- โ Proper error handling without exposing internals
๐งช Testing
To run the application and see all operations:
# Ensure MariaDB is running
sudo systemctl start mariadb # or: brew services start mariadb
# Create database
mysql -u root -p -e "CREATE DATABASE IF NOT EXISTS pegawai_db;"
# Run the main demo
go run main.go
# Check database tables (optional)
mysql -u root -p pegawai_db -e "SHOW TABLES; DESCRIBE pegawais;"Development Commands
# Build the application
go build
# Run with specific environment
APP_ENV=production go run main.go
# Check dependencies
go mod tidy
go mod verify
# Format code
go fmt ./...๐ค Contributing
- Fork the repository
- Create your feature branch (
git checkout -b feature/AmazingFeature) - Commit your changes (
git commit -m 'Add some AmazingFeature') - Push to the branch (
git push origin feature/AmazingFeature) - Open a Pull Request
๐ License
This project is licensed under the MIT License - see the LICENSE file for details.
๐ Contact
- Author: muslchn
- Repository: go-pegawai
๐ Acknowledgments
- GORM - The fantastic ORM library for Go
- MariaDB - Reliable and scalable database
- godotenv - Environment variable management
- Go community for excellent documentation and support
Note: This application demonstrates Go programming concepts, GORM usage, MariaDB integration, and enterprise-grade configuration management. The application includes production-ready features like connection pooling, environment-based configuration, and comprehensive error handling.