A template for a good start into new Go projects.
Linux/MacOS
- Give rights to execute sh files.
chmod +x initialize.sh build.sh- Prepare the project. Ensure the files located in right directory (Project name)
- Initialize the project
NOTE: The initialize.sh has a parameter "-dg" for deleteing the git connection and the gitignore file.
./initialize.sh -dg # initializes and deletes .git
# or
./initialize.sh # just initializes- Build project for test purposes
./build.shWindows
- Prepare the project. Ensure the files located in right directory (Project name)
- Initialize the project
NOTE: The initialize.sh has a parameter "-dg" for deleteing the git connection and the gitignore file.
REM initializes and deletes .git
initialize.bat -dg
REM or
REM just initializes
initialize.bat - Build project for test purposes
build.batBuild is simple. Just execute following:
Linux:
./build.sh
Windows:
build.bat
Results in following directory structure:
├── build
│ ├── Windows
| | ├── x86
| | | └──ProjectName.exe
| | └── x64
| | └──ProjectName.exe
│ ├── Linux
| | ├── x86
| | | └──ProjectName
| | └── x64
| | └──ProjectName
│ └── MacOS/ProjectName
| └── x64
| └──ProjectName
This Go package provides a simple error handling mechanism using custom error details, including error codes, messages, and severity levels. The package defines error types, associated methods, and utilities for working with error details in a structured way.
The errorhandling package defines the ErrorDetails struct, which contains fields for an error code, message, and severity level. The severity level is represented using a custom type, SeverityLevel, which can be one of several predefined values.
- Define custom errors with error codes, messages, and severity.
- Easily generate error descriptions using reflection.
- Methods to retrieve error code, message, and severity.
To use this package, you can import it into your Go project as follows:
import "path/to/your/repository/errorhandling"For main.go just execute:
./initialize.sh
or
initialize.bat
The core of the package is the ErrorDetails struct. It represents an error with a code, message, and severity level.
package main
import (
"fmt"
"GoTemplate/errorhandling" // Import the errorhandling package
)
func main() {
// Create a new error instance
err := errorhandling.ErrGeneral
// Print error details
fmt.Println(err.ErrorDetails()) // Print all details of the error
fmt.Println(err.ErrorMessage()) // Get the error message
fmt.Println(err.ErrorCode()) // Get the error code in hexadecimal format
fmt.Println(err.ErrorSeverityLevel()) // Get the error severity level
}- Code: An integer representing the error code.
- Message: A string describing the error.
- Severity: A string representing the severity level of the error.
The package includes predefined error instances:
var (
Success = ErrorDetails{
Code: 0x0001,
Message: "Success. No errors occurred.",
Severity: Severity.None,
}
ErrGeneral = ErrorDetails{
Code: 0x0002,
Message: "General error occurred",
Severity: Severity.Low,
}
)The SeverityLevel type defines various severity levels for errors. These levels can be used to categorize the seriousness of the error.
var Severity = struct {
None SeverityLevel
Low SeverityLevel
Medium SeverityLevel
High SeverityLevel
Critical SeverityLevel
}{
None: "None",
Low: "Low",
Medium: "Medium",
High: "High",
Critical: "Critical",
}- None: No error (success).
- Low: A minor error or warning.
- Medium: A moderate error that may require attention.
- High: A serious error that needs prompt resolution.
- Critical: A critical error that requires immediate attention.
package main
import (
"fmt"
"GoTemplate/errorhandling"
)
func main() {
// Create a critical error
criticalError := errorhandling.ErrorDetails{
Code: 0x0003,
Message: "Critical system failure",
Severity: errorhandling.Severity.Critical,
}
// Print error details
fmt.Println(criticalError.ErrorDetails()) // Full error details
fmt.Println(criticalError.ErrorMessage()) // "Critical system failure"
fmt.Println(criticalError.ErrorCode()) // "0x0003"
fmt.Println(criticalError.ErrorSeverityLevel()) // "Critical"
}Returns the error message.
func (e ErrorDetails) ErrorMessage() stringReturns the error code in hexadecimal format (e.g., 0x0002).
func (e ErrorDetails) ErrorCode() stringReturns the severity level as a string.
func (e ErrorDetails) ErrorSeverityLevel() stringGenerates a detailed error string with all the error fields (Code, Message, and Severity), which can be useful for logging or debugging.
func (e ErrorDetails) ErrorDetails() stringpackage main
import (
"fmt"
"GoTemplate/errorhandling"
)
func main() {
// Create a new error instance
err := errorhandling.ErrGeneral
// Output all error details
fmt.Println("Error Details:")
fmt.Println(err.ErrorDetails())
// Output error code
fmt.Println("Error Code:", err.ErrorCode())
// Output error message
fmt.Println("Error Message:", err.ErrorMessage())
// Output error severity
fmt.Println("Error Severity:", err.ErrorSeverityLevel())
}The ErrorDetails struct also utilizes reflection to automatically generate error details. This allows you to easily iterate over all fields of the struct and print their names and values, including the custom fields like Code, Message, and Severity.
Example of using reflection in the ErrorDetails struct:
package main
import (
"fmt"
"reflect"
"GoTemplate/errorhandling"
)
func main() {
// Create a new error instance
err := errorhandling.ErrGeneral
// Use reflection to print all error details
val := reflect.ValueOf(err)
for i := 0; i < val.NumField(); i++ {
field := val.Type().Field(i)
value := val.Field(i)
// Format the output for the Code field
if field.Name == "Code" {
fmt.Printf("%s: 0x%04X\n", field.Name, value.Interface())
} else {
// Print other fields
fmt.Printf("%s: %v\n", field.Name, value.Interface())
}
}
}This package provides a simple but flexible error handling mechanism in Go, allowing you to define custom errors with associated severity levels, messages, and codes. The utility functions and methods make it easy to generate detailed error descriptions and handle errors in a structured way.
Feel free to extend the ErrorDetails struct with more fields or create additional predefined errors as needed. This package is designed to be lightweight and easy to integrate into any Go application.