https://github.com/godcong/dl
Default value generates tools for Golang
https://github.com/godcong/dl
default go injector tag tool
Last synced: 2 months ago
JSON representation
Default value generates tools for Golang
- Host: GitHub
- URL: https://github.com/godcong/dl
- Owner: godcong
- License: mit
- Created: 2024-07-18T11:39:02.000Z (11 months ago)
- Default Branch: main
- Last Pushed: 2024-10-29T08:54:56.000Z (8 months ago)
- Last Synced: 2024-10-29T10:03:18.454Z (8 months ago)
- Topics: default, go, injector, tag, tool
- Language: Go
- Homepage:
- Size: 90.8 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Contributing: .github/CONTRIBUTING.md
- License: LICENSE
- Code of conduct: .github/CODE_OF_CONDUCT.md
- Security: .github/SECURITY.md
Awesome Lists containing this project
README
# DL(Default Loader)
## Overview
DL (Default Loader) is a tool designed to generate and assign default values to fields within Go structs based on tags.
This utility allows you to specify default values for your struct fields using a simple tag syntax,
making it easier to initialize structs with predefined values without having to explicitly set them in your code.---
`defaults.go` is forked from [creasty/defaults](https://github.com/creasty/defaults) and modified to meet the requirements of this project.## Features
- **Tag-based Default Values**: Use the `default` tag to set default values for struct fields.
- **Easy Integration**: Quickly generate methods to load default values into your structs.
- **Efficient Initialization**: Simplifies the initialization process of complex structs by automatically setting
default values.## Installation
To use DL (Default Loader), first install the tool via:
```shell
go install github.com/godcong/dl/cmd@latest
```## Usage
### Step 1: Define Struct with Default Tags
Add the `default` tag to your struct fields to specify their default values:
```go
// example: demo.go
type Demo struct {
Name string `default:"demo"`
}
```### Step 2: Generate Default Value Loading Method
Run DL to generate the necessary loading method for your struct,
using the `-f` flag to specify the file path with filename or directory:```shell
dl -f ./demo.go
```This will generate a `Default() error` method in your struct that initializes the fields with the specified default values.
Below is the generated code example with `Demo` struct:
```go
// Default loads default values for Demo
func (obj *Demo) Default() error {
obj.Name = "demo"
return nil
}
```### Step 3: Load Default Values
In your code, use `dl.Load()` to populate your struct with the default values:
```go
func main() {
demo := &Demo{}
if err := dl.Load(demo); err != nil {
panic(err)
}
// Now 'demo' has its fields initialized with default values.
}
```