G3deon, Inc. Common Go Module

Translations
You can view the documentation of this package in your preferred language:
Frequently Asked Questions (FAQs)
What does Common Package do?
Go common packages contain the utilities frequently used in our applications facilitating testing, scalability, and code maintenance.
Why Common Package?
When developing distributed software like G3deon, you often encounter redundant pieces of code. While this might not pose a problem initially with a few applications, it becomes cumbersome to test and maintain as the number of applications scales up.
The solution we've adopted is to create a package containing commonly used functions and utilities.
Getting started
Before starting to work, you need to follow some steps:
Set up pre-commit Git Hook:
You just need to move the script to the /.git/hooks
folder:
ln -s scripts/pre-commit .git/hooks/pre-commit # Linux Ubuntu
Replace redundant code
Replace redundant code in your application and integrate the package.
Replace uint64 for uid (Unique Identifier)
For example, in G3deon, a 64-byte positive integer is used as an ID. Replace the uint64 type and its generation with the uid type from the uid package.
Before
import (
"fmt"
"math/rand"
"time"
)
type User struct {
ID uint64 `json:"uid"`
}
func main() {
rand := rand.New(rand.NewSource(time.Now().UnixNano()))
randomNumber := rand.Int63()
user := &User{
ID: randomNumber,
}
fmt.Printf("%v", user)
}
After
import (
"fmt"
"go.g3deon.com/pkg/uid"
)
type User struct {
ID *uid.UID `json:"uid"`
}
func main() {
user := &User{
ID: uid.New(),
}
fmt.Printf("%v", user)
}
Replace timestamppb for timestamp
If necessary, replace timestamppb with the timestamp package. It already has the recurring validations when working with timestamppb.
import (
"fmt"
"go.g3deon.com/pkg/timestamp"
)
type Subscription struct {
ExpirationDate *timestamp.Timestamp `json:"expiration_date"`
}
func (s *Subscription) Validate() error {
if valid := s.ExpirationDate.IsValid(); !valid {
return fmt.Errorf("expiration_date is not valid")
}
if future := s.ExpirationDate.IsFuture(); !future {
return fmt.Errorf("expiration date must be a in future")
}
return nil
}
func main() {
sub := &Subscription{
ExpirationDate: timestamp.Now()
}
err := sub.Validate()
if err != nil {
panic(err)
}
fmt.Printf("%v", sub)
}
License
This repository is under the MIT license.
MIT License
Copyright (c) 2024 G3deon.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.