Module System
Harneet supports a comprehensive module system with Go.mod integration for external dependencies and version management.
Overview
The module system provides:
- go.mod integration - Standard Go module files for dependency management
- External module resolution - Automatic downloading and caching of dependencies
- Version management - Semantic versioning support
- Import path mapping - Clean import syntax for external modules
- Module caching - Efficient local caching in
imports/directory
Project Structure
go.mod File
Create a go.mod file in your project root:
Import Syntax
External Modules
| External Modules | |
|---|---|
Local Packages
HMOD Tool
HMOD tool is not needed. We are stil working on bringing go packages to Harneet. Kindly stay tuned. As of today , you dont need HMOD tool
Module Management Tool
Use the hmod tool to manage dependencies:
Initialize a Project
Add Dependencies
Download Dependencies
List Dependencies
Clean Cache
Module Resolution
The module system resolves imports in the following order:
- Standard Library - Built-in modules (fmt, json, os, etc.)
- External Modules - Modules listed in go.mod
- Local Packages - Relative path imports (./package, ../package)
External Module Resolution
For import github.com/example/utils/strings:
- Parse go.mod to find matching module
github.com/example/utils v1.0.0 - Check if module exists in
imports/github.com/example/utils@v1.0.0/ - If not found, download module automatically
- Resolve subpath:
imports/github.com/example/utils@v1.0.0/strings/
Version Management
Semantic Versioning
Harneet follows semantic versioning (semver):
Version Constraints
Currently supported: - Exact versions: v1.0.0 - Pre-release: v1.0.0-alpha.1 - Build metadata: v1.0.0+build.1
Module Downloading
The module manager supports multiple download methods:
Go Module Cache
Git Clone Fallback
Supported Hosting
- GitHub:
github.com/user/repo - GitLab:
gitlab.com/user/repo - Bitbucket:
bitbucket.org/user/repo - Custom: Any git-accessible repository
Module Caching
Downloaded modules are cached in the imports/ directory:
Cache Management
- Automatic: Modules downloaded on first import
- Manual: Use
harneet-mod downloadto pre-download - Cleanup: Use
harneet-mod cleanto remove cache
Package Structure
External modules should follow Harneet package conventions:
Package Declaration
| Package Declaration | |
|---|---|
Exports
Only uppercase identifiers are exported:
| Export Rules | |
|---|---|
Integration Examples
Configuration Management with Viper
| Go Modules Config | |
|---|---|
CLI Tool
| CLI Tool Modules | |
|---|---|
| CLI Tool Main | |
|---|---|
Best Practices
Module Organization
- Single Purpose: Each module should have a clear, single purpose
- Clear Exports: Export only necessary functions and types
- Documentation: Document exported functions and types
- Versioning: Use semantic versioning for releases
Import Management
- Group Imports: Group standard library, external, and local imports
- Meaningful Aliases: Use clear, descriptive import aliases
- Avoid Conflicts: Prevent naming conflicts with aliases
| Import Best Practices | |
|---|---|
Dependency Management
- Pin Versions: Use exact versions in production
- Regular Updates: Keep dependencies updated
- Security: Review dependencies for security issues
- Minimal Dependencies: Only add necessary dependencies
Error Handling
Module Not Found
Solution: Add module to go.mod and run harneet-mod download
Version Conflicts
Solution: Update go.mod with compatible versions
Download Failures
Solution: Check network connectivity and repository access
Advanced Features
Module Replacement
Indirect Dependencies
Build Constraints
Migration Guide
From Local Packages
- Create go.mod file
- Move packages to external repositories
- Update import paths
- Use harneet-mod to manage dependencies
From Other Languages
- Identify equivalent Harneet packages
- Create go.mod with dependencies
- Adapt import syntax
- Update function calls to match Harneet conventions
Troubleshooting
Common Issues
- Import cycles: Reorganize package dependencies
- Version conflicts: Update to compatible versions
- Missing modules: Add to go.mod and download
- Permission errors: Check file system permissions
Debug Mode
Verbose Logging
This module system brings enterprise-level dependency management to Harneet while maintaining simplicity and Go-like familiarity.