Skip to content

Subash-Chand-Thakuri/golang-practice

Repository files navigation

Go Learning Project - Task Manager

This project demonstrates all core Go concepts through a practical task management application.

๐ŸŽฏ Concepts Covered

1. Project Structure

  • Standard Go project layout (cmd/, internal/, pkg/)
  • Module system (go.mod)
  • Package organization

2. Core Language Features

  • โœ… Variables, constants, and types
  • โœ… Functions and methods
  • โœ… Structs and custom types
  • โœ… Pointers
  • โœ… Interfaces
  • โœ… Error handling
  • โœ… Slices and maps
  • โœ… Control structures (if, for, switch)

3. Advanced Features

  • โœ… JSON marshaling/unmarshaling
  • โœ… File I/O operations
  • โœ… Concurrency (goroutines and channels)
  • โœ… Mutex for thread safety
  • โœ… HTTP server and routing
  • โœ… Command-line flags

4. Best Practices

  • โœ… Package organization
  • โœ… Error handling patterns
  • โœ… Testing (_test.go files)
  • โœ… Code structure and separation of concerns

๐Ÿ“ Project Structure

golang-practice/
โ”œโ”€โ”€ cmd/
โ”‚   โ””โ”€โ”€ taskmanager/
โ”‚       โ””โ”€โ”€ main.go          # Application entry point
โ”œโ”€โ”€ internal/
โ”‚   โ”œโ”€โ”€ config/
โ”‚   โ”‚   โ””โ”€โ”€ config.go        # Configuration management
โ”‚   โ”œโ”€โ”€ handlers/
โ”‚   โ”‚   โ””โ”€โ”€ task_handler.go  # HTTP request handlers
โ”‚   โ”œโ”€โ”€ models/
โ”‚   โ”‚   โ””โ”€โ”€ task.go          # Task data model
โ”‚   โ””โ”€โ”€ services/
โ”‚       โ”œโ”€โ”€ task_service.go      # Business logic
โ”‚       โ””โ”€โ”€ task_service_test.go # Unit tests
โ”œโ”€โ”€ pkg/
โ”‚   โ””โ”€โ”€ utils/
โ”‚       โ””โ”€โ”€ demo.go          # Utility functions and demos
โ”œโ”€โ”€ go.mod                   # Go module file
โ”œโ”€โ”€ GOLANG_PROJECT_STRUCTURE.md  # Structure guide
โ””โ”€โ”€ README.md               # This file

๐Ÿš€ Getting Started

Prerequisites

  • Go 1.21 or higher installed
  • Basic terminal knowledge

Installation

  1. Navigate to the project directory:

    cd /home/sucth/Programming/Projects/Golang-Practice
  2. Initialize Go module (if not already done):

    go mod init golang-practice
  3. Download dependencies:

    go mod tidy

๐ŸŽฎ Running the Application

1. Demo Mode (Learn All Concepts)

Shows demonstrations of all Go concepts:

CGO_ENABLED=0 go run cmd/taskmanager/main.go -mode=demo

2. CLI Mode (Command Line Interface)

Interactive command-line interface:

CGO_ENABLED=0 go run cmd/taskmanager/main.go -mode=cli

3. Server Mode (HTTP API)

Starts a REST API server:

CGO_ENABLED=0 go run cmd/taskmanager/main.go -mode=server -port=8080

Note: If you get a CGO error, use CGO_ENABLED=0 before the command. See TROUBLESHOOTING.md for details.

Then test the API:

# Get all tasks
curl http://localhost:8080/tasks

# Create a task
curl -X POST http://localhost:8080/tasks \
  -H "Content-Type: application/json" \
  -d '{"title":"New Task","description":"Task description","priority":"high"}'

# Get a specific task
curl http://localhost:8080/tasks/task-1234567890

# Health check
curl http://localhost:8080/health

๐Ÿ“š Learning Path

Step 1: Understand the Structure

Read GOLANG_PROJECT_STRUCTURE.md to understand Go project organization.

Step 2: Start with Models

Look at internal/models/task.go:

  • Struct definition
  • Methods on structs
  • Stringer interface

Step 3: Learn Services

Check internal/services/task_service.go:

  • Business logic
  • Mutex for concurrency safety
  • File I/O operations
  • JSON operations

Step 4: Explore Handlers

See internal/handlers/task_handler.go:

  • HTTP server setup
  • Request routing
  • JSON encoding/decoding
  • Error handling

Step 5: Study Utilities

Review pkg/utils/demo.go:

  • Interfaces
  • Error handling
  • Goroutines and channels
  • Pointers
  • Collections (slices, maps)

Step 6: Run Tests

go test ./internal/services/...

๐Ÿ”‘ Key Go Concepts Explained

1. Packages and Imports

  • Each directory is a package
  • package main is the entry point
  • Import paths use module name

2. Structs and Methods

type Task struct {
    ID    string
    Title string
}

func (t *Task) Complete() {
    // Method on Task
}

3. Interfaces

type Shape interface {
    Area() float64
}

4. Error Handling

result, err := someFunction()
if err != nil {
    return err
}

5. Concurrency

go worker()  // Goroutine
ch := make(chan string)  // Channel

6. JSON Operations

json.Marshal(data)    // Convert to JSON
json.Unmarshal(data, &result)  // Parse JSON

๐Ÿงช Testing

Run all tests:

go test ./...

Run with coverage:

go test -cover ./...

๐Ÿ“ Next Steps

After understanding this project, you can:

  1. Add Features:

    • User authentication
    • Task categories/tags
    • Task due dates
    • Task search and filtering
  2. Improve:

    • Add database (PostgreSQL, MySQL)
    • Add middleware (logging, authentication)
    • Add WebSocket support
    • Add Docker containerization
  3. Build New Projects:

    • REST API with database
    • CLI tool
    • Web scraper
    • Microservice

๐Ÿ› ๏ธ Building

Build executable:

go build -o taskmanager cmd/taskmanager/main.go

Run the executable:

./taskmanager -mode=server

๐Ÿ“– Additional Resources

๐Ÿ’ก Tips

  1. Read the code - Each file demonstrates specific concepts
  2. Run the demo mode - See all concepts in action
  3. Modify and experiment - Change code to see what happens
  4. Write tests - Practice test-driven development
  5. Build something - Apply what you learned

Happy coding! ๐Ÿš€

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published